search within multidimentional array

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
If I have a multidimentional array that contains many arrays inside as

$voters=array(array('name'=>'Joe', 'profession'=>'blumbing',
array('name'=>'Obama', 'profession'=>'senator', 'state'=>'Ilinois'),
array('name'=>'Palin', 'profession'=>'moose hunter',
array('name'=>'Jackson', 'profession'=>'singer',

and I want to search for all the people in the state of 'New York',
for example, to look up their professions. Could someone show me the
best way to do this? TIA

Re: search within multidimentional array

On Fri, 7 Nov 2008 09:59:11 -0800 (PST), wrote:

Quoted text here. Click to load it

$professions = array() ;
foreach ( $voters as $entry )  // entry is, of course, an array
    if ( $entry['state'] == 'New York' )
        $professions[] = $entry['profession'] ;

yields an array of all the professions in NY.

if you want to count the practitioners instead, you could do

$professions = array() ;
foreach( $voters as $entry )
    $professions[ $entry['profession'] ] = 0 ;  

foreach ( $voters as $entry )
    if ( $entry['state'] == 'New York' )
        $professions[ $entry['profession'] ] ++ ;

Re: search within multidimentional array

Quoted text here. Click to load it
 Thanks for your help, A Reader. Now let say at an orientation, each
of the 1000 career-minded first-year college students' career goal is
matched to the profession of his/her mentor, who is contained in the
$voters array and from the student's home state, and if there is a
match the student is provided with the name of his/her mentor.
Assuming each proffesion in $voters array is unique (non-duplicated).

I was thinking of 2 ways to do this as follows:

First approach: Putting the entries in $voters array into arrays of
their states. For example, do the followings for 50 states
$voters_in_NY=3Darray(); $voters_in_PA=3Darray(); // etc. for 50 states
$professions = array() ;
foreach ( $voters as $entry )
        if ( $entry['state'] =3D 'New York' )
                $voters_in_NY[] = array('name'=3D>$entry['name'],
'$profession'=3D$entry['profession']) ;
        if ( $entry['state'] =3D 'Pennsylvania' )
                $voters_in_PA[] = array('name'=3D>$entry['name'],
'$profession'=3D$entry['profession']) ;

// then looping through the student group to do the matching

$students=3Darray(array('name'=3D>'John', 'career goal'=3D>'chemist', 'home
state'=3D>'NY'), array('name'=3D>'Jane', 'career goal'=3D>'nurse', 'home
state'=3D>'PA')); //contains up to 50,000 students
foreach($students as $student) {
 if($student['home state']=3D=3D'PA') //repeat for 50 states
   foreach($voters_in_PA as $voter_in_PA)
     if($student['career goal']=3D=3D$voter_in_PA['profession']) echo

 if($student['home state']=3D=3D'NY') echo $voters_in_NY['name'];

Second approach: Similar to the first approach except for using
multidimenational array instead of multiple single arrays. i.e,
parsing through $voters array, instead of $voters_in_NY, $voters_in_PA
etc., for each student. This increases the number of redundant
parsings but reduce the number of code lines.

Are there array operations (for single or multidimentional arrays)
that simplify this task? I was thinking along the line of somehow
sorting the $voters array according to 'state' and using some sort of
pointer (like the seek() operation, if any) to compare the value with
'home state' so that redundant parsing for each search would be

Could someone show me if this is possible at all of if there is other
simpler or better way to accomplish this task? TIA.

Re: search within multidimentional array wrote:
Quoted text here. Click to load it

This all would be so much easier to do with a SQL database.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: search within multidimentional array

Quoted text here. Click to load it

I think I understand exactly what you meant. Thanks for pointing me to
that direction.

Re: search within multidimentional array

Quoted text here. Click to load it

WHAT is multidimenTional"?

Site Timeline