Add values to an associative array in PHP

前端 未结 2 1003
花落未央
花落未央 2020-12-29 18:26

I want to append an element to to end of an associative array.

For example, my array is

$test=Array ([chemical] => asdasd [chemical_hazards] =>         


        
相关标签:
2条回答
  • 2020-12-29 19:17

    You can do this with PHP's array_merge function.

    $test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); 
    $test2 = array('solution' => 'good');
    $result = array_merge($test, $test2);
    var_dump($result);
    
    0 讨论(0)
  • 2020-12-29 19:29

    Just add it like you would with a non-associative array:

    $test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); //init
    $test['solution'] = 'good';
    
    0 讨论(0)
提交回复
热议问题