Forum Moderators: coopster

Message Too Old, No Replies

Searching an array with an array?

         

fintan

3:16 pm on Oct 13, 2005 (gmt 0)

10+ Year Member



I've been banging my head against this one for awhile. One array has numbers like awz001 for keys and the count of how many times they appear as values. There's about 300 key, value pairs in this array.

So

Array = ([awz001] => 7, etc...)

and the other array is a multi-dimensional

Array ( [0] => Array ( [number] => awz001 [0] => awz001 [Section] => Section one [1] => Section one )

Is there away to search the second one with the first ones array keys?

Would you do it with two loops? First through the multi then the single?

StupidScript

4:47 pm on Oct 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe I'd do 1 foreach, 1 for and 1 in_array ...

foreach($array1 as $key1 => $val1) {

for($i=0;$i<sizeof($array2);$i++) {

if (in_array($key1,$array2[$i])) {

[$array2[$i] array contains $key1]

break 2;

}

}

}

You could also try

foreach($array1 as $key1 => $val1) {

for($i=0;$i<sizeof($array2);$i++) {

$checkit=array_search($key1,$array2[$i]);

if ($checkit) {

[$checkit = matching $array2[$i] key]

break 2;

}

}

}

With

array_search()
it's important to note that if the array being searched uses
0
as it's first key that you use a string
'0'
instead of the numeral/boolean value.

Apparently you can't do nested foreach() loops.

Also note that foreach() makes a copy of the array being tested, so you may experience memory issues with an especially large source array.

fintan

10:48 am on Oct 14, 2005 (gmt 0)

10+ Year Member



Thanks StupidScript for the reply I'll give it a go and see how it pans out.