PHP Notes: The Use of PHP array-related functions _php instance

Source: Internet
Author: User

Creating an array stops saying the array () function

The array can be extracted using the list ().

To test an array element, you can use Is_array ()

Takes a parameter, passes in the variable, if the variable is an array, returns true if it is not returned false.

Adding and removing array elements

FIFO and LIFO of arrays

Let's explain what FIFO and LIFO are.

The deletion element is the same as the order in which elements are added, known as FIFO, both first-in-first-out

The deletion of elements is in contrast to the order in which elements are added, called LIFO, both Last-in-first-out

Operation of the corresponding array

    • Array_unshift ()
    • int Aray_unshift (array array,mixed variable [, mixed variable ...])

Adding elements to the array header, all existing numeric keys are changed to reflect the new position in the array, and the association key is unaffected.

Cases:

<?php

$words = Array (' A ', ' B ', ' C ');

Print_r ($words);

Array ([0]->a,[1]->b,[2]->c);

Array_unshift ($words, ' d ');

Print_r ($words);

Array ([0]->d,[1]->a,[2]->b,[3]->c);

    • Array_shift ()
    • Mixed Array_shift (array array)

Deletes an element from the array header, deletes and returns the element found in the array, and the result is that if the value key is used, all corresponding values are moved down. If you are using an association key, the array is not affected.

Cases:

<?php
$words = Array (' A ', ' B ', ' C ');

Print_r ($words);

Array ([0]->a,[1]->b,[2]->c);

Array_shift ($words, ' a ');

Print_r ($words);

Array ([0]->b,[1]->c);

    • Array_push ()
    • int Array_push (array array, mixed variable [, mixed variable ...])

Adds an element from the end of an array, adds variable to the end of the array, and returns false for success. You can pass multiple arguments as input and press multiple variables into the array at the same time.

Cases:

<?php
$stack = array("orange""banana");
array_push($stack"apple""raspberry");
print_r($stack);
?>

This example will make $stack 具有如下单元:

Array ([0] => Orange [1] => banana [2] => Apple [3] => Raspberry)

-------------------------------------------------------------

The example above is excerpted from the PHP manual. For array operations, using $array[]= can increase efficiency. Because you don't need to call a function.

--------------------the smart split line-----------------------------

    • Array_pop ()
    • Mixed Array_pop (array target_array)

Deletes an element from the end of the array and returns the last element of the array.

<?php
$stack = array("orange""banana""apple""raspberry");
$fruit array_pop($stack);
print_r($stack);
?>

After this operation,$stack 将只有 3 个单元:

Array ([0] => Orange [1] => banana [2] => Apple)

------------------------------------------------------

The example above is excerpted from the PHP manual

-------------------------the smart split line-----------------

Own some summary and point of view.

The above four functions, sometimes can be very useful, first of all say return value

Note that the return value of Array_unshift () and Array_push () should be boolean, but the manual and related books are labeled Int. This is worth thinking about.

The Array_shift () and Array_pop () two functions can not only implement elements that delete the related array, but also return the elements of the array that were deleted. Write here first, there are a lot of back.

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.