我对数组和多维数组很新,我想知道如何比较两个数组的key =>值对(或者包含数组的两个不同键的单个数组?)并取消设置key =>每个数组中与键和值匹配的值。
示例数组:
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
)
)
因此,from
是基本键数组,to
是比较键数组。
我想遍历to
和from
个键组,找到匹配的键,并比较两个匹配项的值。
如果key =>值对匹配,则取消设置to
和from
键阵列中的key =>值。
如果to
中找到的密钥不在from
中,请将其保留在to
数组中。但是,如果在from
中找不到to
中找不到的密钥,请将其从from
密钥数组中取消设置。
将上述数组转换为:
Array
(
[from] => Array
(
[group_name] => test adgrp
[zone_id] => 12
)
[to] => Array
(
[group_name] => test adgroup
[zone_id] => 2
[group_email] => test@group.com
)
)
答案 0 :(得分:1)
@Meta尝试以下概念:
<?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