PHP presses the element into the array header (array

Source: Internet
Author: User

Adding elements in PHP like an array is more common than the Array_push function, which is an array of stack operations, but this function is to add elements to the end of the array, if you need to press the array elements to the head of the array (that is, the first element) how to operate?

Using the PHP built-in function array_unshift, the following is an enumeration of the use of the Array_unshift function.

Array_unshift

The function is to insert one or more elements at the beginning of the array. Added elements as a whole add the same order of elements in the array as in the parameters. All the numeric key names in the modified array are modified to count from zero, and all the text keys remain unchanged. The 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 ("Alixixi", ". com");
Array_unshift ($queue, "apple", "raspberry");
Print_r ($queue);

The above routines will output:

Array
(
[0] => Apple
[1] => Raspberry
[2] => Alixixi
[3] =>. com
)

Articles that you may be interested in

    • PHP finds whether a value exists in the array (In_array (), Array_search (), array_key_exists ())
    • PHP removes null-valued elements from the array (array_filter)
    • Two-dimensional array removal of duplicate values and Array_unique functions
    • How do I remove an element from a PHP array (unset,array_splice)?
    • PHP generates a continuous number (letter) Array function range () analysis, PHP lottery program function
    • The difference between the PHP merge array + and the Array_merge
    • A program in which PHP gets all the files in the directory and saves the results to an array
    • PHP resolution URL (parse_url) parameter array (PARSE_STR)


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.