Php pushes an element to the array header (array_unshift usage)

Source: Internet
Author: User

In php, The array_push function is commonly used to add elements to an array, that is, the array's inbound stack operation. However, this function adds elements to the end of the array, if you want to press an array element into the array header (that is, the first element), how can this problem be solved?

Use the php built-in function array_unshift. The following describes how to use array_unshift.

Array_unshift

This function inserts one or more elements at the beginning of an 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. The names of all numeric keys in the modified array are changed to count again from scratch, and the names of all text keys remain unchanged. This function returns the number of elements in the array after the operation.

Syntax: int array_unshift (array & $ array, mixed $ var [, mixed $...])

For example:

<?php$queue = array("phpernote",".com");array_unshift($queue,"apple","raspberry");print_r($queue);

The above routine will output:

Array
(
[0] => apple
[1] => raspberry
[2] => phpernote
[3] =>. com
)

Articles you may be interested in
  • Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
  • Remove duplicate values and array_unique functions from two-dimensional arrays
  • PHP merges arrays + and array_merge
  • PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
  • Php obtains all the files in the directory and saves the results to the array program.
  • PHP rounding precision decimal places and Rounding Methods
  • How does php Delete the first and last elements of an array?
  • How to insert an element to the front of the body element in js

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.