array key and array_push()

Discussion in 'PHP' started by Khal3d, Jan 20, 2010.

  1. #1
    Hello,

    I need to use array_push() to add some data to another array But the problem i can't add key to array for example

    <?php
    $array = array('name' => 'Khaled Attia');
    $add = array('Age' => '19');
    array_push($array, $add);
    print_r($array);
    ?>
    
    PHP:
    output:
    Array
    (
        [name] => Khaled Attia
        [0] => Array
            (
                [Age] => 19
            )
     
    )
    Code (markup):
    But I need output like it
    Array
    (
        [name] => Khaled Attia
        [Age] => 19
     
    )
    Code (markup):

    How can I do this???

    Thanks
     
    Khal3d, Jan 20, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $array['Age'] = 19;
    Code (markup):
     
    SmallPotatoes, Jan 20, 2010 IP
  3. Khal3d

    Khal3d Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I need use array_push()
    because pushing will be in custom function
    and array variable not in function and i use "global" to get this Variable in function and add data

    for example

    <?php
    $var = array('name' => 'Khaled'); 
    function add_data(){
    	global $var;
    	$add = array('Age' => '19');
    	array_push($var, $add);
    }
    add_data();
    print_r($var);
    ?>
    
    PHP:
     
    Khal3d, Jan 20, 2010 IP
  4. Khal3d

    Khal3d Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php
    $var = array('name' => 'Khaled'); 
    function add_data(){
        global $var;
        $var['Age'] = 19;
    }
    add_data();
    print_r($var);
    ?>
    PHP:
    working fine, but there is anyway to use array_push() ?
     
    Khal3d, Jan 20, 2010 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, array_push does not preserve keys.

    You can just do:
    
    $add = array('Age' => 19);
    list ($k, $v) = each($add);
    $var[$k] = $v;
    
    Code (markup):
     
    SmallPotatoes, Jan 20, 2010 IP
  6. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #6
    Hi,
    you can use array_push of course but better should be passing the global array var as function parameter.
    <?php
    $var = array('name' => 'Khaled');
    function add_data($value){
        $value['Age'] = 19;
        return;
    }
    add_data(&$var);
    print_r($var);
    ?>
    
    PHP:
    Regards,
    Nick
     
    koko5, Jan 20, 2010 IP
  7. Khal3d

    Khal3d Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok, Thank you so much :)
     
    Khal3d, Jan 20, 2010 IP