How to Insert an Item At the Beginning of an Array in PHP

How to insert an item at the beginning of an array in PHP?

Use array_unshift($array, $item);

$arr = array('item2', 'item3', 'item4');
array_unshift($arr , 'item1');
print_r($arr);

will give you

Array
(
[0] => item1
[1] => item2
[2] => item3
[3] => item4
)

Insert new item in array on any position in PHP

You may find this a little more intuitive. It only requires one function call to array_splice:

$original = array( 'a', 'b', 'c', 'd', 'e' );
$inserted = array( 'x' ); // not necessarily an array, see manual quote

array_splice( $original, 3, 0, $inserted ); // splice in at position 3
// $original is now a b c x d e

If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself, an object or NULL.

RETURN VALUE: To be noted that the function does not return the desired substitution. The $original is passed by reference and edited in place. See the expression array &$array with & in the parameters list .

PHP - add item to beginning of associative array

You could use the union operator:

$arr1 = array('key0' => 'value0') + $arr1;

or array_merge.

Adding new values to start of an array in PHP

array_unshift() is the function you are looking for!

array_unshift — Prepend one or more elements to the beginning of an array

$arr = array(1,2,3);
print_r($arr);

/*
Array
(
[0] => 1
[1] => 2
[2] => 3
)
*/
array_unshift($arr,0);
print_r($arr);

/*
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
)
*/

Add new array at the beginning of multidimensional array

Use array_unshift():

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

array_unshift($noti_log, array('Greetings'));

How to insert element into arrays at specific position?

array_slice() can be used to extract parts of the array, and the union array operator (+) can recombine the parts.

$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array)-3, true);

This example:

$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);

gives:


Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)

how to insert item at the beginning of a associative array in php

Because you are using an associative array, you need to use array_merge to prepend the series data (i.e. data indexed by $name to the beginning of the $data[$d['playingDate']] array. This code will do what you want. Note that I've put the series code at the end to avoid having to check for $data[$d['playingDate']] being set as well as $data[$d['playingDate']][$name].

$data = [];
foreach($allmatches as $d){
$name = $d['div']['divisionName'];
$data[$d['playingDate']]['day']=$d['day'];
$data[$d['playingDate']]['month']=$d['month'];
$data[$d['playingDate']]['isToday']=$d['isToday'];
if (!isset($data[$d['playingDate']][$name])) $data[$d['playingDate']] = array_merge(array($name => array()), $data[$d['playingDate']]);
$data[$d['playingDate']][$name][]=$d;
}

I've created a small demo on 3v4l.org

Array push as the first index PHP

http://php.net/manual/en/function.array-unshift.php

array_unshift($array, "Choose City")

or you can do it manually

Can't add an item as first item in array - php array_unshift()

replace this

$this->data['dropdown_items'] = array_unshift($this->data['dropdown_items'], "Please select a category");

with

array_unshift($this->data['dropdown_items'], "Please select a category");

array_unshit returns number of elements in array that`s why you are getting array count

Adding an element to the beginning of an array without changing other array keys

If you use self-assigned (e.g. literal) keys, array_unshift() will do it.

If you use auto-generated (numeric) keys, how should that work? Use '-1' as the new first key?

EDIT:

Thank you to JasonS for pointing out an error in this answer.

ANY numeric key will be re-indexed by array_unshift(), no matter if it was auto-generated or self-assigned - if it's numeric, it'll get scrambled. See the link to the documentation above for details.



Related Topics



Leave a reply



Submit