Commonly used array functions and methods in PHP are: 1. The "array_push()" function adds one or more elements to the end of the array; 2. The "count()" function returns the number of elements in the array; 3. The "array_pop()" function deletes and returns the elements at the end of the array; 4. The "array_shift()" function deletes and returns the elements at the beginning of the array; 5. The "sort()" function sorts the array in ascending order.
Operating system for this tutorial: Windows 10 system, php8.1.3 version, Dell G3 computer.
Commonly used array functions and methods in PHP are:
1. array_push() adds one or more elements to the end of the array
Example :
$my_array = array("apple", "banana"); array_push($my_array, "orange", "peach"); print_r($my_array);
The result is:
Array ( [0] => apple [1] => banana [2] => orange [3] => peach )
2. count() returns the number of elements in the array
Example:
$my_array = array("apple", "banana", "orange"); $count = count($my_array); echo "The count of my_array is " . $count;
The result is:
The count of my_array is 3
3. array_pop() deletes and returns the element at the end of the array
Example:
$my_array = array("apple", "banana", "orange"); $last_element = array_pop($my_array); print_r($my_array); echo "The last element was " . $last_element;
Result For:
Array ( [0] => apple [1] => banana ) The last element was orange
4, array_shift() deletes and returns the element at the beginning of the array
Example:
$my_array = array("apple", "banana", "orange"); $first_element = array_shift($my_array); print_r($my_array); echo "The first element was " . $first_element;
The result is:
Array ( [0] => banana [1] => orange ) The first element was apple
5. sort() sorts the array in ascending order
Example:
$my_array = array(9, 2, 5, 3, 8); sort($my_array); print_r($my_array);
The result is:
Array ( [0] => 2 [1] => 3 [2] => 5 [3] => 8 )
The above is the detailed content of What are the commonly used array functions and methods in PHP?. For more information, please follow other related articles on the PHP Chinese website!