On Jan 15, 2009, at 9:29 PM, Wade Preston Shearer wrote:

But if my food array looks like this…

$food[] = array('id' => 1, 'name' => 'banana');
$food[] = array('id' => 2, 'name' => 'taco');
$food[] = array('id' => 3, 'name' => 'toast');


…it's not possible unless you do this first…

foreach($food as $item) {
        $food_notmulti[$item['id']] = $item['name'];
}


…to flatten it.

Or am I wrong? Is there a way to "pluck" from the multi-dimensional array where the ID is not an index?



One approach might be:

foreach ( $food as $index => $row ) {
        foreach ( $row as $key => $value ) {
                if ( $value == 'taco' ) {
                        $found_taco = $food[$index];
                        print_r( $found_taco );
                }
        }
}

If you don't know value needed to directly reference the data then you'll have to loop through until you find it. I suspect folks have already written recursive array search functions that would do this in fairly generic way.


--
Joseph Scott
[email protected]
http://josephscott.org/






_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to