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

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);

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; 

Tags:

Php