Php array function sequence-array_unshift () insert one or more elements at the beginning of the array _ PHP Tutorial

Source: Internet
Author: User
In the php array function sequence, array_unshift () inserts one or more elements at the beginning of the array. Array_unshift () definition and usage the array_unshift () function inserts one or more elements at the beginning of the array. The added elements are added as a whole. The order of these elements in the array and Array_unshift () definition and usage
The array_unshift () function inserts one or more elements at the beginning of the array.

The added elements are added as a whole. The order of these elements in the array is the same as that in the parameter.

This function returns the number of elements in the array.

Syntax
Array_unshift (array, value1, value2, value3.) parameter description
Array is required. Specifies the input array.
Value1 is required. Specifies the value to be inserted.
Value2 is optional. Specifies the value to be inserted.
Value3 is optional. Specifies the value to be inserted.

Tips and comments
Note: the names of all numeric keys are changed to count again from scratch, and the names of all string keys remain unchanged.

Example 1

The code is as follows:


$ A = array ("a" => "Cat", "B" => "Dog ");
Array_unshift ($ a, "Horse ");
Print_r ($ );
?>


Output:

Array ([0] => Horse [a] => Cat [B] => Dog) Example 2
Return key value:

The code is as follows:


$ A = array ("a" => "Cat", "B" => "Dog ");
Print_r (array_unshift ($ a, "Horse "));
?>


Output:

Example 3
The array has a numeric key:

The code is as follows:


$ A = array (0 => "Cat", 1 => "Dog ");
Array_unshift ($ a, "Horse ");
Print_r ($ );
?>


Output:

Array ([0] => Horse [1] => Cat [2] => Dog)

The http://www.bkjia.com/PHPjc/324613.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324613.htmlTechArticlearray_unshift () definition and usage array_unshift () function inserts one or more elements at the beginning of the array. The added elements are added as a whole. The order of these elements in the array is as follows...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.