View Full Version : multidimensional array from foreach -- stumped

03-16-2009, 08:55 PM
Hey All,
It's been a while... having withdrawls! Honestly though, I have beat my head against this little bit of code for far longer than I care to admit. Deflated, hat in hand, bruised head hung in shame, I confess to needing a little help. My question is, how to get from this...
$fruit= array('apples','oranges');
$FruitsAndVegs= array('apples'=>"red",'oranges'=>"orange",'eggplant'=>"purple",);
?> to this...?
Array (
[apples] => Array ( [type] => fruit [color] => red )
[oranges] => Array ( [type] => fruit [color] => orange )
[eggplant] => Array ( [type] => vegetable [color] => purple )
)here's my code as it stands now
$fruit= array('apples','oranges');
$FruitsAndVegs= array('apples'=>"red",'oranges'=>"oranage",'eggplant'=>"purple",);
foreach($FruitsAndVegs as $key =>$value){
if (in_array($key, $fruit)){
$item[type]= 'fruit';//produce type
$item[type]= 'vegetable';//produce type
$item[color]= $value;//produce color

03-16-2009, 09:14 PM
$fruit = array('apples', 'oranges');
$FruitsAndVegs = array('apples' => 'red', 'oranges' => 'orange', 'eggplant' => 'purple');

foreach ($FruitsAndVegs as $key => $value)
if (in_array($key, $fruit)) {$array['type'] = 'fruit';} else {$array['type'] = 'vegetable';}
$array['color'] = $value;
$produce[$key] = $array;

echo '<pre>'; print_r($produce); echo '</pre>';

03-16-2009, 09:30 PM
OMG! (and I NEVER say that -- nor LOL either), but what a total brain fart!
Must be Monday?
Thanks devinemke. Got my fix:thumbsup: