The Array_unshift () function inserts one or more elements at the beginning of the array. Added elements as a whole, the order of these elements in the array and the order in the parameters
Array_unshift () Definition and usage
The Array_unshift () function inserts one or more elements at the beginning of the array.
The added elements, as a whole, are in the order of the elements in the array and in the order of the parameters.
The function returns the number of elements in the array.
Grammar
Array_unshift (Array,value1,value2,value3 ...) Parameter description
Array required. Specifies the input array.
Value1 required. Specifies the value to insert.
value2 is optional. Specifies the value to insert.
Value3 is optional. Specifies the value to insert.
Hints and notes
Note: All numeric key names are modified to count back from zero, and all string key names remain unchanged.
Example 1
Copy CodeThe code is as follows:
<?php
$a =array ("a" and "Cat", "b" = "Dog");
Array_unshift ($a, "Horse");
Print_r ($a);
?>
Output:
Array ([0] = Horse [A] = Cat [b] = Dog) Example 2
Return key value:
Copy CodeThe code is as follows:
<?php
$a =array ("a" and "Cat", "b" = "Dog");
Print_r (Array_unshift ($a, "Horse"));
?>
Output:
3 Example 3
Array with numeric keys:
Copy CodeThe code is as follows:
<?php
$a =array (0=> "Cat",1=> "Dog");
Array_unshift ($a, "Horse");
Print_r ($a);
?>
Output:
Array ([0] = Horse [1] = Cat [2] = Dog)
PHP Array function sequence Array_unshift () inserts one or more elements at the beginning of the array