php notes: the use of php array related functions

  • 2020-06-01 08:44:23
  • OfStack

I'm not going to say array() when I create an array

You can extract the array using list().

Test array elements with is_array()

Take 1 argument, pass in the variable, if the variable is an array, return true if not false.

Add and remove array elements

Arrays fifo and lifo

What are fifo and lifo

Removing elements in the same order as adding them is called fifo, which means first-in-first-out

Removing elements in the opposite order as adding them is called lifo, which is last-in-first-out

The corresponding operation on the array

array_unshift() int aray_unshift(array array,mixed variable [,mixed variable...])

When an element is added to the array header, all existing numeric keys are changed to reflect the new position in the array, and the associated key is not affected.

Ex. :

< ?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 the element from the array header, deletes and returns the element found in the array, with the result that if the numeric key is used, all the corresponding values will be moved down. If the associative key is used, the array will not be affected.

Ex. :

< ?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...])

Add an element from the end of the array, add variable to the end of the array, return true on success, return false on failure. You can pass multiple arguments as input and press multiple variables into the array at the same time.

Ex. :

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

This example will enable $stack to have the following units:

Array ( [0] = > orange [1] = > banana [2] = > apple [3] = > raspberry )

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

The above example is from the PHP manual. For array operations, use $array[]= to increase efficiency. No function calls are required.

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- clever line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

array_pop() mixed array_pop(array target_array)

Removes the 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 will have only 3 units:

Array ( [0] = > orange [1] = > banana [2] = > apple )

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

The above example is taken from the PHP manual

-- clever dividing line --

Own 1 some summary and viewpoint.

These four functions, which can sometimes be very useful, start with the return value

Note that the return values for array_unshift () and array_push () should be Boolean, but the manual and related books indicate that they return int. This is worth considering.

The array_shift() and array_pop() functions not only remove the elements of the associated array, but also return the deleted array elements.


Related articles: