首页 > 作文

PHP数组array类常见操作示例

更新时间:2023-04-08 18:20:24 阅读: 评论:0

本文实例讲述了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);//计算数组的差集(对比返回在 arraons经历y1 中但是不在 array2 及任何其它参数数组中的值。)

例:

<?php$array1 = array("a" => "green", "red", "blue", "red");$array2 = arra李爱民y("b" => "green", "yellow", "r世界人口ed");$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, $offt, $length);//从数组中取出一段

参数:array::输入的数组, offt:序列偏移量, 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"

本文发布于:2023-04-08 18:20:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/4c1d00d5bce2974e2ef4dfbfcfd12e14.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP数组array类常见操作示例.doc

本文 PDF 下载地址:PHP数组array类常见操作示例.pdf

标签:数组   组中   例程   参数
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图