Home >Backend Development >PHP Tutorial >array_slice and array_flip in php array functions

array_slice and array_flip in php array functions

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-28 08:26:411387browse

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 "<pre class="brush:php;toolbar:false">";
  var_dump($Obj);
  echo "
"; }
Let’s look at the array_flip function:
//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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn