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

Detailed explanation of PHP array function examples

WBOY
Release: 2023-06-20 09:12:01
Original
1238 people have browsed it

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, ...);
Copy after login

Example:

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

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => watermelon
)
Copy after login
  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);
Copy after login

Example:

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

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login
  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);
Copy after login

Example:

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

Output:

Array
(
    [0] => banana
    [1] => orange
    [2] => watermelon
)
Copy after login
  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, ...);
Copy after login

Example:

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

Output:

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

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

array_reverse($array);
Copy after login

Example:

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

Output:

Array
(
    [0] => watermelon
    [1] => orange
    [2] => banana
    [3] => apple
)
Copy after login
  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);
Copy after login

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);
Copy after login

Output:

Array
(
    [0] => banana
    [1] => orange
)
Copy after login
  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);
Copy after login

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);
Copy after login

Output:

Array
(
    [0] => apple
    [1] => kiwi
    [2] => grape
    [3] => watermelon
)
Copy after login
  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);
Copy after login

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 "不存在";
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template