[UPHPU] pluck out of array with index without rebuilding
Joseph Scott
joseph at randomnetworks.com
Fri Jan 16 11:06:31 MST 2009
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
joseph at josephscott.org
http://josephscott.org/
More information about the UPHPU
mailing list