As a revolutionary web development language, PHP has many powerful features and functions, among which array functions are one of them. Arrays are one of the most common data types in PHP and are widely used in various web applications.
In this article, we will introduce some PHP array functions and their application in actual development. We will divide the function into the following parts according to its purpose: array sorting function, array processing function, array query function, and array merging function.
1. Array sorting function
$numbers = array(5, 3, 8, 1); sort($numbers); print_r($numbers);
The output result is: Array([0] => 1,[1] => 3,[2] => 5,[3] => 8)
$numbers = array(5, 3, 8, 1); rsort($numbers); print_r($numbers);
The output result is: Array([0] => 8,[1] => 5,[2] => 3,[3] => 1)
$numbers = array("A"=>5, "B"=>3, "C"=>8, "D"=>1); asort($numbers); print_r($numbers);
The output result is: Array([D] => 1,[B] => 3,[A] => 5,[C] => 8)
$numbers = array("A"=>5, "B"=>3, "C"=>8, "D"=>1); arsort($numbers); print_r($numbers);
The output result is: Array([C] => 8,[A] => 5,[B] => 3,[D] => 1)
2. Array processing function
$colors = array("red", "green"); array_push($colors, "blue", "yellow"); print_r($colors);
The output result is: Array([0] => red,[1] => green,[2] => blue,[3] => yellow)
$colors = array("red", "green", "blue"); $lastColor = array_pop($colors); print_r($colors); echo $lastColor;
The output result is: Array([0] => red,[1] => green)
blue
$colors = array("red", "green", "blue"); $firstColor = array_shift($colors); print_r($colors); echo $firstColor;
The output result is: Array([0] => green,[1] => blue)
red
$colors = array("red", "green"); array_unshift($colors, "blue", "yellow"); print_r($colors);
The output result is: Array([0] => blue,[1] => yellow,[2] => red,[3] => green)
3. Array query function
$colors = array("red", "green", "blue"); if (in_array("green", $colors)) { echo "找到了"; } else { echo "没找到"; }
The output result is: found
$colors = array("red", "green", "blue"); $pos = array_search("green", $colors); echo $pos;
The output result is: 1
$colors = array("red", "green", "blue"); if (array_key_exists(1, $colors)) { echo "存在"; } else { echo "不存在"; }
The output result is: exists
4. Array merge function
$colors1 = array("red", "green"); $colors2 = array("blue", "yellow"); $colors = array_merge($colors1, $colors2); print_r($colors);
The output result is: Array([0] => red,[1] => green,[2] => blue,[3] => yellow)
$keys = array("A", "B", "C"); $values = array("red", "green", "blue"); $colors = array_combine($keys, $values); print_r($colors);
The output result is: Array([A] => red,[B] => green,[C] => blue)
As can be seen from the above example, the array functions provided by PHP are very convenient and can help us quickly implement various array operations. In actual development, we should make full use of these functions to improve development efficiency and code quality.
The above is the detailed content of How to use php array function. For more information, please follow other related articles on the PHP Chinese website!