php 数组的合并、拆分、区别取值函数集

前端之家收集整理的这篇文章主要介绍了php 数组的合并、拆分、区别取值函数集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

合并数组有三个函数

1.array_combine()

携带两个参数数组,参数数组一的值作新数组的键,参数数组二的值作新数组的值。很简单。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="62670" class="copybut" id="copybut62670" onclick="doCopy('code62670')"> 代码如下:
<div class="codebody" id="code62670">
<?PHP
$a = array('green','red','yellow');
$b = array('avocado','apple','banana');
$c = array_combine($a,$b); print_r($c);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="48152" class="copybut" id="copybut48152" onclick="doCopy('code48152')"> 代码如下:
<div class="codebody" id="code48152">
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)

2.array_merge()

携带两个参数数组,简单的将数组二追加到数组一的后面构成新数组。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="26126" class="copybut" id="copybut26126" onclick="doCopy('code26126')"> 代码如下:
<div class="codebody" id="code26126">
<?PHP
$array1 = array("color" => "red",2,4);
$array2 = array("a","b","color" => "green","shape" => "trapezoid",4);
$result = array_merge($array1,$array2);
print_r($result);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="65647" class="copybut" id="copybut65647" onclick="doCopy('code65647')"> 代码如下:
<div class="codebody" id="code65647">
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)

3.array_merge_recursive()

与上面函数雷同,唯一的区别是在追加时发现要添加的键已存在时,array_merge()的处理方式是覆盖前面的键值,array_merge_recursive()的处理方式是重构子数组,将重复的键的值组成一个新的数值数组。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="47084" class="copybut" id="copybut47084" onclick="doCopy('code47084')"> 代码如下:<div class="codebody" id="code47084">
<?PHP
$ar1 = array("color" => array("favorite" => "red"),5);
$ar2 = array(10,"color" => array("favorite" => "green","blue"));
$result = array_merge_recursive($ar1,$ar2);
?>

上例将输出 $result:
<div class="codetitle"><a style="CURSOR: pointer" data="29331" class="copybut" id="copybut29331" onclick="doCopy('code29331')"> 代码如下:<div class="codebody" id="code29331">
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
) [0] => blue
) [0] => 5
[1] => 10
)

拆分数组有两个函数

1.array_slice()

携带三个参数,参数一为目标数组,参数二为offset,参数三为length。作用为,从目标数组中取出从offset开始长度为length的子数组。 如果offset为正数,则开始位置从数组开头查offset处,如果offset为负数开始位置从距数组末尾查offset处。如果length为正数,则毫无疑问取出的子数组元素个数为length,如果length为负数,则子数组从offset开始到距数组开头count(目标数组)-|length|处结束。特殊地,如果length为空,则结束位置在数组结尾。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="56034" class="copybut" id="copybut56034" onclick="doCopy('code56034')"> 代码如下:<div class="codebody" id="code56034">
<?PHP
$input = array("a","c","d","e"); $output = array_slice($input,2); // returns "c",and "e"
$output = array_slice($input,-2,1); // returns "d"
$output = array_slice($input,3); // returns "a",and "c" // note the differences in the array keys
print_r(array_slice($input,-1));
print_r(array_slice($input,-1,true));
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="75924" class="copybut" id="copybut75924" onclick="doCopy('code75924')"> 代码如下:<div class="codebody" id="code75924">
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)

2.array_splice()

携带三个参数,同上,作用是删除从offset开始长度为length的子数组。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="42812" class="copybut" id="copybut42812" onclick="doCopy('code42812')"> 代码如下:<div class="codebody" id="code42812">
<?PHP
$input = array("red","green","blue","yellow");
array_splice($input,2);
// $input is now array("red","green") $input = array("red",1,-1);
// $input is now array("red","yellow") $input = array("red",count($input),"orange");
// $input is now array("red","orange") $input = array("red",array("black","maroon"));
// $input is now array("red",
// "blue","black","maroon") $input = array("red",3,"purple");
// $input is now array("red","purple","yellow");
?>

区别取值函数有四个:

1.array_intersect()


携带参数不定,均为数组,返回所有数组中公共元素的值组成的数组,数组的键由所在第一个数组的键给出。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="42148" class="copybut" id="copybut42148" onclick="doCopy('code42148')"> 代码如下:<div class="codebody" id="code42148">
<?PHP
$array1 = array("a" => "green","red","blue");
$array2 = array("b" => "green","yellow","red");
$result = array_intersect($array1,$array2);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="17581" class="copybut" id="copybut17581" onclick="doCopy('code17581')"> 代码如下:<div class="codebody" id="code17581">
Array
(
[a] => green
[0] => red
)

2.array_intersect_assoc()

在前一个函数的基础上,返回所有数组中键、值均相同的键值对。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="91282" class="copybut" id="copybut91282" onclick="doCopy('code91282')"> 代码如下:<div class="codebody" id="code91282">
<?PHP
$array1 = array("a" => "green","b" => "brown","c" => "blue","red");
$array2 = array("a" => "green","red");
$result_array = array_intersect_assoc($array1,$array2);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="43621" class="copybut" id="copybut43621" onclick="doCopy('code43621')"> 代码如下:<div class="codebody" id="code43621">
Array
(
[a] => green
)

3.array_diff()

携带多个数组,返回第一个数组中有的而后面的数组中没有的所有的值组成的新数组,对应键取自第一个数组。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="86605" class="copybut" id="copybut86605" onclick="doCopy('code86605')"> 代码如下:<div class="codebody" id="code86605">
<?PHP
$array1 = array("a" => "green","red");
$array2 = array("b" => "green","red");
$result = array_diff($array1,$array2); print_r($result);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="10768" class="copybut" id="copybut10768" onclick="doCopy('code10768')"> 代码如下:<div class="codebody" id="code10768">
Array
(
[1] => blue
)

4.array_diff_assoc()

在前一个函数的基础上,不仅需要匹配值还要匹配键。 例子:
<div class="codetitle"><a style="CURSOR: pointer" data="38280" class="copybut" id="copybut38280" onclick="doCopy('code38280')"> 代码如下:<div class="codebody" id="code38280">
<?PHP
$array1 = array ("a" => "green","red");
$array2 = array ("a" => "green","red");
$result = array_diff_assoc($array1,$array2);
?>

上例将输出
<div class="codetitle"><a style="CURSOR: pointer" data="2627" class="copybut" id="copybut2627" onclick="doCopy('code2627')"> 代码如下:<div class="codebody" id="code2627">
Array
(
[b] => brown
[c] => blue
[0] => red
)

猜你在找的PHP相关文章