开发者

Insert at start of array a key-value pair in php

array_unshift ($ids,$product_id => $catalog_ta开发者_如何学Gog);

if i put

array($product_id => $catalog_tag) 

would work but wont add $product_id as key.. I want to add it at the start


Use array_reverse to reverse the array and then push the element to end of array using array_push and then reverse the array again. You will have the new element at beginning of the array.

or

$arrayone=array("newkey"=>"newvalue") + $arrayone; 


This code takes your array noting that it's using integer keys for your array so these will always be the array order, so we convert them to string keys and this prevents the key's acting as the order of the array. we then create the a new array with your value and append all values from your existing array to the end.

note that the product ID is converted to a string again to prevent the integer key from ordering the array.

$ids = array(0 => "bla bla", 1 => "bla bla", 2 => "bla bla", 3 => "bla bla")
foreach($ids as $key => $val){
    $key = "$key";
}
unset(current($ids));
$ids = array_merge(array("$product_id" => $catalog_tag), $ids);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜