each($arr) array
Return a pair of key/value that is, subscript/value in the re-array $arr
Copy the codeThe code is as follows:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => c);
$res = each($arr);
print_r($res); //The result is array('0' => 'a', 'key' => 'a', 1 => 1, 'value' => 1)
//Return result 0 and key both represent subscript 1 and value represent values
//If the internal pointer of each array is not used once, it will move downward once. If it is already the end of the array, false will be returned.
$res = each($arr);
print_r($res); //The result will become array('0' => 'b', 'key' => 'b', 1 => 2, 'value' => 2)
?>
$arr = array('a' => 1, 'b' => 2, 'c' => c);
$res = each($arr);
print_r($res); //The result is array('0' => 'a', 'key' => 'a', 1 => 1, 'value' => 1)
//Return result 0 and key both represent subscript 1 and value represent values
//If the internal pointer of each array is not used once, it will move downward once. If it is already the end of the array, false will be returned.
$res = each($arr);
print_r($res); //The result will become array('0' => 'b', 'key' => 'b', 1 => 2, 'value' => 2)
?>
list($val, .. , [$val]) void No return value
Assign the value in the array to some variable $val
Copy the codeThe code is as follows:
<?php
$arr = array('color', 'letter A', 'letter B');
list($color, $a, $b) = $arr; //Note that the parameter $val position of the list here corresponds to the position in the array, and relies on left to right
echo $color; //Result 'color'
echo $a; //Result 'Letter A'
list($color, , $b) = $arr; //This is empty
echo $color; //Result 'color'
echo $a; //The result is not the same, because a is not assigned a value, and it is empty anywhere.
echo $b; //Result 'Letter B'
//List each
$arr = array('color', 'letter A', 'letter B');
while(list($key, $val) = each($arr)) {
echo 'Subscript:' . $key;
echo '----value:' . $val;
echo '<br>';
}
//The output result is
//Subscript: 0----Value: Color
//Subscript: 1----Value: Letter A
//Subscript: 2----Value: Letter B
?>
krsort($arr, [$type]) bool
Arrange the array $arr in reverse order according to its subscript
There is also a ksort(); this is arranged in ascending order by subscript
$type is the sorting method
Copy the codeThe code is as follows:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
krsort($arr);
print_r($arr); //Print result: array('c' => 3, 'b' => 2, 'a' => 1);
?>
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
krsort($arr);
print_r($arr); //Print result: array('c' => 3, 'b' => 2, 'a' => 1);
?>
range($go, $end, [$setup) array
This is a bit difficult to explain
Copy the codeThe code is as follows:
<?php
$arr = range(1, 10);
print_r($arr); //Result array(1, 2, 3, 4, .., 10);
$arr = range(1, 10, 2); //Specify the third parameter here, that is, step size
print_r($arr); //Result array(1, 3, 5, 7, 9);
$arr = range('a', 'z');
print_r($arr); //Result array('a', 'b', 'c', ..., 'z');
?>
sort($arr);
Sorting the array over a-z
Copy the codeThe code is as follows:
<?php
$arr = array("lemon","orange","banana","apple");
sort($arr);
print_r($arr); //Result array('apple', 'banana', 'lemon', 'orange');
//There is also a function rsort for sorting z-a
?>
$arr = array("lemon","orange","banana","apple");
sort($arr);
print_r($arr); //Result array('apple', 'banana', 'lemon', 'orange');
//There is also a function rsort for sorting z-a
?>
shuffle($arr) bool
Sorting the array $arr randomly
Copy the codeThe code is as follows:
<?php
$arr = range(1, 10);
shuffle($arr);
print_r($arr); //I don't know what the result is, because it's random
?>
usort($arr, $func) bool
Use your own defined function to sort $arr $func is a custom function
Copy the codeThe code is as follows:
<?php
$arr = array(1, 5, 8 ,2 ,0 ,3);
usort($arr, 'test');
function test($a, $b) {
return $a == $b ? 0 : $a < $b ? 1 : -1;
}
print_r($arr); //Print result Array ( [0] => 8 [1] => 5 [2] => 3 [3] => 2 [4] => 1 [5] => 0 )
// I didn't understand this function. . Don't know what the parameters $a and $b represent? I didn't understand the manual, if you want to sort it in such a simple way, just use ksort
// There are two other functions like this, uksort, and uasort, and I'm depressed. . .
?>
Let's summarize
Copy the codeThe code is as follows:
<?php
array_values() //Return all elements in the array
array_walk() //Use custom functions to traverse the array all in one unit
arsort(); //Reserve the relationship in which the array is subscripted to the value and arranges it in reverse order
asort() //Reserve the relationship in which the array is subscripted to the value for positive order
compact() //Import the variable into an array, use the variable name as the subscript, and the value is the value
extract() //Import the array to a variable, the subscript is the variable name, and the value is the value
count() //Statistics how many elements there are in the array
current() //The element pointed by the current pointer
next() //The pointer moves down
prev() //The pointer moves upward
end() //The pointer moves to the end of the array
reset() // move the pointer to the beginning of the array
key() //Return the subscript of the element pointed to by the current pointer
each() //Return a pair of key/value
list() //Import array into variable
krsort() //Sort in reverse order according to subscript
ksort() //Arrange ascending order according to subscript
range() //Create an array
sort() //arrange by value small to large
rsort() //arrange by value to some
shuffle() //Random arrangement
usort() //......Speechless function, go to Google
?>
Previous page12Read the full text