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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘