array_unshift ($ids,$product_id => $catalog_tag);
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
The array_unshift() function inserts new elements to an array. The new array values will be inserted in the beginning of the array. Tip: You can add one value, or as many as you like. Note: Numeric keys will start at 0 and increase by 1.
To add a key/value pair to all objects in an array:Use the Array. forEach() method to iterate over the array. On each iteration, use dot notation to add a key/value pair to the current object. The key/value pair will get added to all objects in the array.
A simple solution to insert an item at a specific position in an array is using the array_slice() function. The idea is to extract a slice of the array using array_slice() function and then recombine the parts using the array_merge() function.
The array_keys() function returns an array containing the keys.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With