开发者

How put same element on a first place in associative array, using a php?

开发者 https://www.devze.com 2023-03-06 09:39 出处:网络
I\'ve a assoc array like array(\"id\"=>\"1\",\"name\"=>\"NiLL\"); and I need to add first element in this array. My finally array must be thi开发者_StackOverflow社区s array(\"error\" => \"0\"

I've a assoc array like array("id"=>"1","name"=>"NiLL"); and I need to add first element in this array. My finally array must be thi开发者_StackOverflow社区s array("error" => "0", "id"=>"1","name"=>"NiLL"); How I can do this, with out overwrite array?


Just use documentation:

function array_unshift_assoc(&$arr, $key, $val) 
{ 
    $arr = array_reverse($arr, true); 
    $arr[$key] = $val; 
    $arr = array_reverse($arr, true); 
    return count($arr); 
}

In this case:

$your_array = array("id"=>"1","name"=>"NiLL");
array_unshift_assoc($your_array, 'error', '0');


You could use array_merge():

array_merge( array("Error" => 0), $other_array );

Your first parameter will be an array with the key/value you wish to insert into your other array.

0

精彩评论

暂无评论...
验证码 换一张
取 消