Home  >  Article  >  Backend Development  >  Detailed explanation of PHP array function examples

Detailed explanation of PHP array function examples

WBOY
WBOYOriginal
2023-06-20 09:02:371185browse

Array functions in PHP are very useful for processing arrays. In this article, we will take a closer look at some of the most commonly used array functions.

  1. array_push()

The array_push() function can push one or more elements to the end of the array. The syntax is as follows:

array_push($array, $value1, $value2, ...);

Example:

$fruits = array("apple", "banana");
array_push($fruits, "orange", "watermelon");
print_r($fruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => watermelon
)
  1. array_pop()

array_pop() function can pop An element at the end of the array and returns the value of that element. The syntax is as follows:

array_pop($array);

Example:

$fruits = array("apple", "banana", "orange", "watermelon");
$pop = array_pop($fruits);
echo $pop; //输出:watermelon
print_r($fruits);

Output:

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

array_shift() function can Removes the element at the beginning of the array and returns the value of that element. The syntax is as follows:

array_shift($array);

Example:

$fruits = array("apple", "banana", "orange", "watermelon");
$shift = array_shift($fruits);
echo $shift; //输出:apple
print_r($fruits);

Output:

Array
(
    [0] => banana
    [1] => orange
    [2] => watermelon
)
  1. array_unshift()

array_unshift() function can One or more elements are added to the beginning of the array. The syntax is as follows:

array_unshift($array, $value1, $value2, ...);

Example:

$fruits = array("apple", "banana", "orange");
array_unshift($fruits, "watermelon", "kiwi");
print_r($fruits);

Output:

Array
(
    [0] => watermelon
    [1] => kiwi
    [2] => apple
    [3] => banana
    [4] => orange
)
  1. array_reverse()

array_reverse() function can The order of elements in the array is reversed. The syntax is as follows:

array_reverse($array);

Example:

$fruits = array("apple", "banana", "orange", "watermelon");
$reverse_fruits = array_reverse($fruits);
print_r($reverse_fruits);

Output:

Array
(
    [0] => watermelon
    [1] => orange
    [2] => banana
    [3] => apple
)
  1. array_slice()

array_slice() function can be obtained from Get a fragment from an array. The syntax is as follows:

array_slice($array, $offset, $length);

Among them, $offset indicates the position where slicing is to begin, and $length indicates the length of slicing.

Example:

$fruits = array("apple", "banana", "orange", "watermelon");
$sliced_fruits = array_slice($fruits, 1, 2);
print_r($sliced_fruits);

Output:

Array
(
    [0] => banana
    [1] => orange
)
  1. array_splice()

array_splice() function can replace or delete elements in an array A fragment into which new elements can be inserted. The syntax is as follows:

array_splice($array, $offset, $length, $replace_array);

Among them, $offset represents the position where the operation is to be started, $length represents the number of elements to be replaced or deleted, and $replace_array represents the element to be inserted. If no new elements need to be inserted, the $replace_array parameter can be omitted.

Example:

$fruits = array("apple", "banana", "orange", "watermelon");
array_splice($fruits, 1, 2, array("kiwi", "grape"));
print_r($fruits);

Output:

Array
(
    [0] => apple
    [1] => kiwi
    [2] => grape
    [3] => watermelon
)
  1. array_key_exists()

array_key_exists() function checks whether an array exists The specified key. The syntax is as follows:

array_key_exists($key, $array);

Among them, $key is the key to be checked, and $array is the array to be checked.

Example:

$fruits = array("apple" => 1, "banana" => 2, "orange" => 3);
if (array_key_exists("banana", $fruits)) {
    echo "存在";
} else {
    echo "不存在";
}

Output: exists

In addition, there are many other array functions, such as array_map(), array_filter(), array_reduce(), etc. Mastering these functions allows us to process arrays in PHP more efficiently.

The above is the detailed content of Detailed explanation of PHP array function examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn