dongwuxie5112 2017-02-21 23:48
浏览 34
已采纳

比较两个数组并使用php取消数组中的任何匹配对?

I am pretty new with arrays and multi-dimensional arrays, and am wondering how to go about comparing the key=>value pairs of two arrays (or a single array with two different keys that contain arrays?) and unset the key=>value pairings from each array that match keys and value.

Example Array:

Array
(
    [from] => Array
        (
            [active] => 1
            [airport_ids] => 
            [group_name] => test adgrp
            [zone_id] => 12
            [creation_time] => 1234567890
        )

    [to] => Array
        (
            [active] => 1
            [airport_ids] => 
            [group_name] => test adgroup
            [zone_id] => 2
            [group_email] => test@group.com
        )

)

So, from is the base key array and to is the comparison key array. I want to iterate through both to and from key arrays, finding matching keys, and comparing the values of the two matches.

If they key=>value pair matches, unset the key=>value from both to and from key arrays.

if there is a key found in to that is not in from, leave it in the to array. However, if there is a key found in from that is not found in to, unset it from the from key array.

Turning the above array into something like this:

Array
(
    [from] => Array
        (
            [group_name] => test adgrp
            [zone_id] => 12
        )

    [to] => Array
        (
            [group_name] => test adgroup
            [zone_id] => 2
            [group_email] => test@group.com
        )

)
  • 写回答

1条回答 默认 最新

  • duankanjian4642 2017-02-22 05:46
    关注

    @Meta try this below concept:

    <?php
        $arr1 = 
                array
                (
                    "from" => array
                        (
                            "active" => 1,
                            "airport_ids" => null,
                            "group_name" => "test adgrp",
                            "zone_id" => 12,
                            "creation_time" => 1234567890
                        ),
    
                    "to" => array
                        (
                            "active" => 1,
                            "airport_ids" => null,
                            "group_name" => "test adgroup",
                            "zone_id" => 2,
                            "group_email" => "test@group.com"
                        )
    
                );
    
        echo "<pre>";
        print_r($arr1); //before
    
        foreach($arr1["from"] as $key => $value) {
          foreach($arr1["to"] as $key1 => $value1) {
            if($key == $key1 && $value == $value1){
                unset($arr1["from"][$key], $arr1["to"][$key1]);
                break;
            }
    
          }
        }
        echo "<pre>";
        print_r($arr1); //after
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用