Home  >  Article  >  Backend Development  >  What are the php array functions?

What are the php array functions?

PHPz
PHPzOriginal
2023-04-18 09:46:17469browse

PHP is a widely used server-side scripting language. It supports many array functions for processing array-related operations, such as traversing, sorting, merging, etc. Today we will introduce some commonly used PHP array functions so that you can make full use of these functions to process arrays.

  1. array() function
    array() function is used to create a new array. The function can receive any number of arguments, which become elements of the array. Let’s look at an example:
$array = array("apple", "banana", "cherry");

creates an array with three elements, apple, banana and cherry. This function also supports the creation of associative arrays, for example:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

In this way, you can store multiple keys and values ​​in an array and easily retrieve them when needed.

  1. count() function
    count() function is used to return the length of the array. It accepts an array parameter and returns the number of elements in the array. For example:
$array = array('apple', 'banana', 'cherry');
echo count($array);

This should output 3 because the array contains three elements.

  1. array_push() and array_pop() functions
    The array_push() function is used to add one or more elements to the end of the array, while the array_pop() function is used to remove from the end of the array element. For example:
$array = array('apple', 'banana', 'cherry');
array_push($array, 'orange'); // 添加一个元素到数组的末尾
echo count($array); // 输出4,因为现在该数组包含了4个元素
$fruit = array_pop($array); // 从数组的末尾删除元素,并将其赋值给 $fruit
echo $fruit; // 输出"orange"
  1. array_shift() and array_unshift() functions
    Similar to the array_push() and array_pop() functions, the array_shift() function is used to remove elements from the beginning of the array, And the array_unshift() function is used to add one or more elements to the beginning of the array. For example:
$numbers = array(1, 2, 3, 4, 5);
$first = array_shift($numbers); // 从数组的开头删除元素,并将其赋值给 $first
echo $first; // 输出1
array_unshift($numbers, 0); // 在数组的开头添加一个元素
echo count($numbers); // 输出5,因为现在该数组包含了5个元素
  1. array_merge() function
    array_merge() function is used to merge two or more arrays into one array. For example:
$array1 = array('apple', 'banana', 'cherry');
$array2 = array('orange', 'lemon', 'lime');
$new_array = array_merge($array1, $array2);
echo count($new_array); // 输出6,因为新数组中包含了6个元素
  1. array_reverse() function
    array_reverse() function reverses the order of elements in an array and returns a new array. For example:
$array = array('apple', 'banana', 'cherry');
$new_array = array_reverse($array);
echo $new_array[0]; // 输出"cherry"

The above are six commonly used PHP array functions. Of course, PHP has many other useful functions, you can check the official PHP documentation for more details. Once you master these basic array functions, you will be able to handle and manipulate PHP arrays more easily.

The above is the detailed content of What are the php array functions?. 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