Array is a commonly used data type in PHP. In order to facilitate the operation and processing of arrays, PHP provides many built-in array functions. This article will introduce some commonly used PHP array functions.
1. array() function
array() function is a function that creates an array in PHP. Its syntax is as follows:
array(value1,value2,value3. ..)
Where value1, value2, value3... are values, which can be any PHP variable type.
Example:
$fruits = array("apple", "banana", "orange");
$numbers = array(1, 2, 3, 4, 5) ;
2. count() function
The count() function is a function in PHP used to return the number of array elements. Its syntax is as follows:
count( array_name)
where array_name is the array whose number of elements needs to be calculated.
Example:
$fruits = array("apple", "banana", "orange");
echo count($fruits); //Output 3
3. Sort() function
The sort() function is a function in PHP used to sort arrays. Its syntax is as follows:
sort(array_name,sort_type)
array_name is the array that needs to be sorted, sort_type indicates the sorting method, and the value can be SORT_REGULAR, SORT_NUMERIC or SORT_STRING.
Example:
$numbers = array(1, 5, 3, 7, 2);
sort($numbers);
print_r($numbers); // Output Array ([0] => 1 [1] => 2 [2] => 3 [3] => 5 [4] => 7 )
4. shuffle() Function
shuffle() function is a function in PHP used to randomly sort arrays. Its syntax is as follows:
shuffle(array_name)
where array_name needs to be random Sorted array.
Example:
$numbers = array(1, 5, 3, 7, 2);
shuffle($numbers);
print_r($numbers); // Output Array ([0] => 7 [1] => 3 [2] => 1 [3] => 2 [4] => 5 )
5. array_merge() Function
array_merge() function is a function in PHP used to merge multiple arrays into a new array. Its syntax is as follows:
array_merge(array1,array2,array3... )
Among them, array1, array2, array3... are the arrays that need to be merged.
Example:
$array1 = array("name"=>"Tom","age"=>20);
$array2 = array("name"= >"Jerry","age"=>21);
$new_array = array_merge($array1,$array2);
print_r($new_array); //Output Array ( [name] => Jerry [age] => 21 )
6. in_array() function
The in_array() function is a function in PHP used to determine whether a value exists in an array. The syntax is as follows:
in_array(value,array_name)
where value is the value to be found and array_name is the array to be found.
Example:
$fruits = array("apple", "banana", "orange");
if(in_array("banana", $fruits)){
echo "找到了!";
} else {
echo "没找到!";
}
7. array_search() function
array_search() function is used in PHP to find a value in an array and A function that returns its key name. Its syntax is as follows:
array_search(value,array_name)
where value is the value to be searched, and array_name is the array to be searched.
Example:
$fruits = array("apple", "banana", "orange");
echo array_search("banana", $fruits); //Output 1
The above is an introduction to some PHP array functions. These functions can help us process arrays more conveniently and improve the development efficiency and quality of PHP.
The above is the detailed content of Some commonly used PHP array functions. For more information, please follow other related articles on the PHP Chinese website!