Append into associative array

Discussion in 'PHP' started by daimous, Jan 22, 2006.

  1. #1
    Can any body pls. help me...here's my problem..
    How will I append another value in a associative array using foreach? let say i have this array with a value.
    $words = array($x => array($rand_cell_number,$direction));
    PHP:
    any help will greatly appreciated. thanks in advance!
     
    daimous, Jan 22, 2006 IP
  2. Salsa

    Salsa Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You may have already figured this out by now. But, if not, see if this helps.

    Because you want to use foreach, I assume you already have your data in simple arrays, and you want to combined them into a single multi-dimensional array. Using info from another of your posts, try something like this:

    <?php
    $ages = array('Sue'=>'30','Art'=>'40','John'=>'50','Mike'=>'60');
    $genders = array('John'=>'M','Mike'=>'M','Sue'=>'F','Art'=>'M');
    
    foreach ($ages AS $name =>$age) $people[$name]['age'] = $age; 
    foreach ($genders AS $name =>$gender) $people[$name]['gender'] = $gender; 
    ?>
    <pre><?php print_r($people) ?></pre>
    Code (markup):
    I hope this helps,
    Salsa
    _______
     
    Salsa, Jan 24, 2006 IP