您好,登录后才能下订单哦!
这篇文章主要讲解了“如何操作PHP数组array类”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何操作PHP数组array类”吧!
array_merge($arr1,$arr2....);//合并一个或多个数组
例:
1、
<?php $beginning = 'foo'; $end = array(1 => 'bar'); $result = array_merge((array)$beginning, (array)$end); print_r($result); ?>
以上例程会输出:
Array
(
[0] => foo
[1] => bar
)
2、
<?php $array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); print_r($result); ?>
以上例程会输出:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
3、
<?php $array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2); ?>
别忘了数字键名将会被重新编号!
Array
(
[0] => data
)
4、
<?php $array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a'); $array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b'); $result = $array1 + $array2; var_dump($result); ?>
运行结果:
array(5) {
[0]=>
string(6) "zero_a"
[2]=>
string(5) "two_a"
[3]=>
string(7) "three_a"
[1]=>
string(5) "one_b"
[4]=>
string(6) "four_b"
}
array_diff($arr1,$arr2);//计算数组的差集(对比返回在 array1 中但是不在 array2 及任何其它参数数组中的值。)
例:
<?php $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result); ?>
在 $array1 中多次出现的值一样处理,输出结果为:
Array
(
[1] => blue
)
array_diff_assoc($arr1,$arr2...);//带索引检查计算数组的差集
array_diff_assoc() 返回一个数组,该数组包括了所有在 array1 中但是不在任何其它参数数组中的值
<?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result); ?>
以上例程会输出:
Array
(
[b] => brown
[c] => blue
[0] => red
)
array_keys($array);//返回数组中部分的或所有的键名(返回 input 数组中的数字或者字符串的键名。)
<?php $array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array)); ?>
以上例程会输出:
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
array_slice($array, $offset, $length);//从数组中取出一段
参数:array::输入的数组, offset:序列偏移量, length:单元长度
$input = array("a", "b", "c", "d", "e"); $output = array_slice($input, 2); // returns "c", "d", and "e" $output = array_slice($input, -2, 1); // returns "d" $output = array_slice($input, 0, 3); // returns "a", "b", and "c"
感谢各位的阅读,以上就是“如何操作PHP数组array类”的内容了,经过本文的学习后,相信大家对如何操作PHP数组array类这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。