On Fri, Jan 16, 2009 at 11:06 AM, Joseph Scott
<[email protected]> wrote:
>
> 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');
Had that same issue .. I wrote a function and go about it like this:
<?php
$food[] = array('id' => 1, 'name' => 'banana');
$food[] = array('id' => 2, 'name' => 'taco');
$food[] = array('id' => 3, 'name' => 'toast');
print_r($food);
// Using 'name' key
print_r(setIndex($food,'name'));
// Using 'id' key
print_r(setIndex($food,'id'));
/**
* setIndex
*
* @description reindex an array to one of the key values
* @returns array
**/
function setIndex($array,$newIdx)
{
$newArray = array();
foreach($array as $value)
{
$newArray[$value[$newIdx]] = $value;
}
return($newArray);
}
?>
--
--
-- Matthew Frederico
_______________________________________________
UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net