Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts

    multidimensional array from foreach -- stumped

    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 Code:
    <?php
    $fruit
    = array('apples','oranges');
    $FruitsAndVegs= array('apples'=>"red",'oranges'=>"orange",'eggplant'=>"purple",);
    ?>
    to this...?
    PHP Code:
    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 Code:
    <?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);
    ?>
    Last edited by fuzzy1; 03-16-2009 at 08:02 PM.

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    $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>'

  • Users who have thanked devinemke for this post:

    fuzzy1 (03-16-2009)

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    OMG! (and I NEVER say that -- nor LOL either), but what a total brain fart!
    Must be Monday?
    Thanks devinemke. Got my fix


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •