Look at array_slice first.
array_slice is used to take out a part of the array. There are two ways to use it:
$arr=array('a'=>'aa', 'b'=>'bb', 'c'=>'cc', 'd'=>'dd'); //从数组中第1个数据(数组本身从0开始)开始,取两项。 $tmp=array_slice($arr,1,2); showObj($tmp); //从数组倒数第三个数据开始,往后取两项: $tmp=array_slice($arr,-3,3); showObj($tmp);
Next, look at the output result:
array(2) {
["b"]=>
string (2) "bb"
["c"]=>
string(2) "cc"
}
array(3) {
["b"]=>
string(2) "bb"
["c"]=>
string(2) "cc"
["d"]=>
string(2) "dd"
}
The showObj function is a package of var_dump, used to print specific Object.
function showObj($Obj) { echo ""; var_dump($Obj); echo ""; }Copy after login
//array_flip用来交换数组中的键值对 $arr=array_flip($arr); showObj($arr);
Output result:
array(4) {
["aa"]=>
string(1) "a"
["bb"]=>
string(1) "b"
["cc "]=>
string(1) "c"
["dd"]=>
string(1) "d"
}
The above introduces array_slice and array_flip in the PHP array function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.