...

View Full Version : multidimensional array from foreach -- stumped



fuzzy1
03-16-2009, 07: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...
<?php
$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
<?php
$fruit= array('apples','oranges');
$FruitsAndVegs= array('apples'=>"red",'oranges'=>"oranage",'eggplant'=>"purple",);
$p=0;
foreach($FruitsAndVegs as $key =>$value){
if (in_array($key, $fruit)){
$item[type]= 'fruit';//produce type
}
else{
$item[type]= 'vegetable';//produce type
}
$item[color]= $value;//produce color
$produce[$key]=var_export($item);
}
print_r($produce);
?>

devinemke
03-16-2009, 08: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>';

fuzzy1
03-16-2009, 08: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:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum