PHP开发笔记系列(九)- 数组(四)

    经过《PHP开发笔记系列(九)- 数组(一)》《PHP开发笔记系列(九)- 数组(二)》《PHP开发笔记系列(九)- 数组(三)》 三篇的关于数组的使用后,本篇《PHP开发笔记系列(九)- 数组(四)》 将把Php数组的最后一部分,数组的集合处理。


    1. 合并数组

    在平常的使用中,我们可能需要从数据库中查询两部分数据,分别是两个数组,然后融合两个数组得到一个结果。要达到这样的效果,需要用到array_merge()函数,对于数值键数组,两个数组中的元素会叠加,即使值相同,也不会覆盖前面的元素,因此返回结果的数组长度等于两个数组之和。如下:

 

file: array_merge.php
url: http://localhost:88/array/array_merge.php
<?php

    $number = array(1, 2, 3, 4, 5);

    $letter = array('a', '1', 'b', '2', 'c');
    
    $result = array_merge($number, $letter);
    
    print_r($result);

?>

 

    对于关联键数组,如果键相同,则对应的值会被后面的元素覆盖。如下:

 

file: array_merge.php
url: http://localhost:88/array/array_merge.php
<?php
    
    $student = array('s1'=>'1st student', 's2'=>'2nd student', 's3'=>'3rd student');
    
    $new_student = array('s1'=>'1st new student', 's2'=>'2nd student', 's3'=>'3rd new student');
    
    $result = array_merge($student, $new_student);
    
    print_r($result)
    
?>

 

    2. 递归追加数组

    array_merge_recursive()函数与array_merge()函数相同,不同的是前者会把键值相同的两个元素的值合并在一起,形成一个新的数组并以原有的键作为数组名。如下:

 

file: array_merge_recursive.php
url: http://localhost:88/array/array_merge_recursive.php
<?php

    $student = array('s1'=>'1st student', 's2'=>'2nd student', 's3'=>'3rd student');
    
    $new_student = array('s1'=>'1st new student', 's2'=>'2nd student', 's3'=>'3rd new student');
    
    $result1 = array_merge_recursive($student, $new_student);
    
    print_r($result1)
    
?>

 

    3. 键数组与值数组合并

    array_combine()函数可以将一个键数组与一个值数组合并,这两个数组需要长度相同,不能为空。如下:

 

file: array_combine.php
url: http://localhost:88/array/array_combine.php
<?php

    $short_name = array('MJ', 'RM', 'PP', 'MC', 'KD');

    $full_name = array('Michael Jackson', 'Rose Mary', 'Peter Pan', 'Mike chan', 'Kelvin Durant');
    
    $result = array_combine($short_name, $full_name);
    
    print_r($result);
    
?>

 

    4. 数组拆分

    array_slice()函数与array_splice()函数接受三个参数,第一个是输入数组,第二个是offset,第三个是length,用于从输入数组截取offset开始,截取length长度的元素,以数组形式返回。length可以为正数或负数。如下:

 

file: array_slice.php
url: http://localhost:88/array/array_slice.php
<?php

    $short_name = array('MJ', 'RM', 'PP', 'MC', 'KD');

    // 从第2为开始,截取2个元素
    $result = array_slice($short_name, 2, 2);
    
    print_r($result);
    
    echo "<br/>==================<br/>";

    // 若length为负数,则截取到count($short_name)-|length|位置结束
    $result1 = array_slice($short_name, 2, -1);
    
    print_r($result1);
    
?>

 

    array_splice()函数与array_slice()函数类似,不同的是前者会删除数组中从offset开始到offset+length结束的所有元素,并以数组形式返回被删除的元素。

 

file: array_splice.php
url: http://localhost:88/array/array_splice.php
<?php

    $short_name = array('MJ', 'RM', 'PP', 'MC', 'KD');

    // 从第2为开始,截取2个元素
    $result = array_splice($short_name, 2, 2);
    
    print_r($result);
    
    echo "<br/>==================<br/>";
    
    $full_name = array('Michael Jackson', 'Rose Mary', 'Peter Pan', 'Mike chan', 'Kelvin Durant');

    // 若length为负数,则截取到count($short_name)-|length|位置结束
    $result1 = array_splice($full_name, 2, -1);
    
    print_r($result1);
    
?>

 

    5. 数组交集

    array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。

 

file: array_intersect.php
url: http://localhost:88/array/array_intersect.php
<?php

    $arr1 = array('MJ', 'RM', 'PP', 'MC', 'KD');
    
    $arr2 = array('MJ', 'RM', 'PP1', 'MC1', 'KD1');
    
    $arr3 = array('MJ', 'RM', 'PP2', 'MC2', 'KD2');
    
    // 比较时,只有两个元素有相同的数据类型时,才被认为相等。
    $intersect = array_intersect($arr1, $arr2, $arr3);
    
    print_r($intersect);
    
?>

 

    求关联数组的交集时,用到的array_intersect_assoc()与array_intersect()相同,只不过在考虑是会考虑数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值才被返回到结果数组中。如下:

 

file: array_intersect_assoc.php
url: http://localhost:88/array?array_intersect_assoc.php
<?php

    $arr1 = array('MJ'=>'Michael Jackson', 'RM'=>'Rose Mary', 'PP'=>'Peter Pan');
    
    $arr2 = array('MJ'=>'Michael Jackson', 'ML'=>'Mary Lee', 'RM'=>'Rose Mary');
    
    // 只有键和值都相等的才会被返回
    $intersect = array_intersect_assoc($arr1, $arr2);
    
    print_r($intersect);
    
?>

 

    6. 数组差集

    array_diff()函数返回出现在第一个数组中但其他输入数组中没有的值。这个函数的功能与array_intersect()相反。

 

file: array_diff.php
url: http://localhost:88/array/array_diff.php
<?php

    $arr1 = array('MJ', 'RM', 'PP', 'MC', 'KD');
    
    $arr2 = array('MJ', 'RM', 'PP1', 'MC1', 'KD1');
    
    $arr3 = array('MJ', 'RM', 'PP2', 'MC2', 'KD2');
    
    $diff = array_diff($arr1, $arr2, $arr3);
    
    print_r($diff);
    
?>

 

    求关联数组的差集时,用到的array_diff_assoc()与array_diff()相同,只不过在考虑是会考虑数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值才被返回到结果数组中。如下:

 

file: array_diff_assoc.php
url: http://localhost:88/array/array_diff_assoc.php
<?php

    $arr1 = array('MJ'=>'Michael Jackson', 'RM'=>'Rose Mary', 'PP'=>'Peter Pan');
    
    $arr2 = array('MJ'=>'Michael Jackson', 'ML'=>'Mary Lee', 'RM'=>'Rose Mary');
    
    // 只有键和值都相等的才会被返回
    $diff = array_diff_assoc($arr1, $arr2);
    
    print_r($diff);
    
?>

 

    还有其他的shuffle()函数、array_chunk()函数、array_sum()函数用得相对较少,就不再讲述。


    本文地址: http://ryan-d.iteye.com/blog/1566777

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值