PHP is a programming language widely used in Web development. Its powerful array processing capabilities are part of its excellent performance. Through the flexible use of arrays, very complex data structures and algorithms can be easily implemented. This article will introduce some of the most commonly used array methods in PHP to help you better understand PHP's array functions and make your development process more efficient and convenient.
This method can be used to compare different values in two arrays and return a new array containing different values. It receives two Or an array as a parameter, the syntax is as follows:
array_diff ( array $array1 , array $array2 [, array $... ] ) : array
Sample code:
$array1 = array("red","blue","green"); $array2 = array("red","yellow","blue"); $result = array_diff($array1, $array2); print_r($result);
Output result:
Array ( [1] => green )
In this example,array_diff()
The method returns different values in$array1
and$array2
. "green" in$array1
does not exist in$array2
, so it is returned as a different value.
This method can be used to filter elements in an array. It receives an array as a parameter, specifies filtering rules through a callback function, and returns The new array, the syntax is as follows:
array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
Sample code:
function myFilter($value) { return strlen($value) > 5; } $array = array("apple", "orange", "banana", "watermelon"); $result = array_filter($array, "myFilter"); print_r($result);
Output result:
Array ( [3] => watermelon )
In this example, thearray_filter()
method passes the callback FunctionmyFilter()
filters the$array
array, leaving only elements whose string length is greater than five characters, so only "watermelon" is retained.
This method creates a new array by performing the same operation on each element in an array. It receives one or more arrays. As parameters, and a callback function, the syntax is as follows:
array_map ( callable $callback , array $array1 [, array $... ] ) : array
Sample code:
function myFunc($value) { return $value * $value; } $array = array(1, 2, 3, 4, 5); $result = array_map("myFunc", $array); print_r($result);
Output result:
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
In this example,array_map()
Method uses callback functionmyFunc()
Performs an operation on each element in the$array
array, and stores the results of all operations in a new array$result
middle.
This method is used to merge two or more arrays together and return a new array that receives two or more Multiple arrays as parameters, the syntax is as follows:
array_merge ( array $array1 [, array $... ] ) : array
Sample code:
$array1 = array("red","green","blue"); $array2 = array("yellow","purple"); $result = array_merge($array1, $array2); print_r($result);
Output result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow [4] => purple )
In this example, thearray_merge()
method will$array1
and$array2
are combined into a new array and the result is stored in$result
.
This method performs dimensionality reduction on the values in the array, iterates the array according to the callback function, and reduces the result to a single value. It receives an array as a parameter and a callback function. The syntax is as follows:
array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) : mixed
Sample code:
function myReduce($carry, $item) { $carry += $item; return $carry; } $array = array(1, 2, 3, 4, 5); $result = array_reduce($array, "myReduce", 0); echo $result;
Output result:
15
In this example,array_reduce The ()
method iterates over each element in the$array
array, uses the callback functionmyReduce()
to reduce the dimension of the array, and returns the result as a single value .
This method is used to find a value in the array and return its corresponding key name. If it does not exist, it returns false. It receives An array and a value as parameters, the syntax is as follows:
array_search ( mixed $needle , array $haystack [, bool $strict = false ] ) : mixed
Sample code:
$array = array("red", "green", "blue"); $key = array_search("green", $array); echo $key;
Output result:
1
In this example,array_search()
Method searches for the value "green" in the$array
array, and returns its corresponding key name 1 after finding it.
This method is used to select a segment of elements from the array and return a new array. It receives an array and a starting position and A length is used as a parameter, and the syntax is as follows:
array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) : array
Sample code:
$array = array("red", "green", "blue", "yellow", "purple"); $result = array_slice($array, 1, 3); print_r($result);
Output result:
Array ( [0] => green [1] => blue [2] => yellow )
In this example, thearray_slice()
method is selected$array
Three elements starting from the second element in the array, store them in the new array$result
.
Summary
In the development process, we often need to process some data collections. PHP's array function can help us easily implement these functions. This article introduces some commonly used array methods, includingarray_diff()
、array_filter()
、array_map()
、array_merge()
、array_reduce()
、array_search()
andarray_slice()
methods, I hope these methods can help readers during the development process.
The above is the detailed content of PHP array method collection. For more information, please follow other related articles on the PHP Chinese website!