我有一个关联数组(name = $ holidays),如下所示。每个键可能会也可能不会指向一个值。如果一个键指向一个值,该值是一个数组,如下所示。 (键[4]指向一个具有键=>值对的数组[override] => 1)
Array
(
[1] =>
[2] =>
[3] =>
[4] => Array
(
[override] => 1
)
[5] =>
[6] =>
[7] =>
)
我有另一个2D关联数组(名称= $天),如下所示。
Array
(
[0] => Array
(
[a] => 1
[b] => 20120903
[c] => 1
[d] => Mon
[e] => 3
[f] => September
[g] => Sep
[h] => 9
[i] => 1
[j] => 1
)
[1] => Array
(
[a] => 2
[b] => 20120904
[c] => 1
[d] => Tue
[e] => 4
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[2] => Array
(
[a] => 3
[b] => 20120905
[c] => 1
[d] => Wed
[e] => 5
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[3] => Array
(
[a] => 4
[b] => 20120906
[c] => 1
[d] => Thu
[e] => 6
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[4] => Array
(
[a] => 5
[b] => 20120907
[c] => 1
[d] => Fri
[e] => 7
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[5] => Array
(
[a] => 6
[b] => 20120908
[c] => 1
[d] => Sat
[e] => 8
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[6] => Array
(
[a] => 7
[b] => 20120909
[c] => 1
[d] => Sun
[e] => 9
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
)
我基本上需要在这两个数组之间进行比较:
获取$ holidays数组的第一个键(让我们将其命名为$ val1)
然后转到$ days数组,访问第一个子数组,获取该子数组的键[a]的值(让它命名为$ val2),然后检查
($ val1和$ val2)是否相同 和
数组$ holiday的键是否包含值(正如我上面提到的$ holiday数组的键可能不指向任何值),
如果这两个条件都是真的 将新的key =>值对添加到$ days数组的相关子数组中 然后转到数组的下一个元素,依此类推。
所以,鉴于上面的2个数组,我希望下面的数组返回。
Array
(
[0] => Array
(
[a] => 1346610600
[b] => 20120903
[c] => 1
[d] => Mon
[e] => 3
[f] => September
[g] => Sep
[h] => 9
[i] => 1
[j] => 1
)
[1] => Array
(
[a] => 1346697000
[b] => 20120904
[c] => 1
[d] => Tue
[e] => 4
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[2] => Array
(
[a] => 1346783400
[b] => 20120905
[c] => 1
[d] => Wed
[e] => 5
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[3] => Array
(
[a] => 1346869800
[b] => 20120906
[c] => 1
[d] => Thu
[e] => 6
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
[k] => 1 <<---------------- This is the new key value pair
)
[4] => Array
(
[a] => 1346956200
[b] => 20120907
[c] => 1
[d] => Fri
[e] => 7
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[5] => Array
(
[a] => 1347042600
[b] => 20120908
[c] => 1
[d] => Sat
[e] => 8
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
[6] => Array
(
[a] => 1347129000
[b] => 20120909
[c] => 1
[d] => Sun
[e] => 9
[f] => September
[g] => Sep
[h] => 9
[i] =>
[j] =>
)
)
希望很清楚。有人可以帮忙吗?提前谢谢。
答案 0 :(得分:0)
foreach($holidays as $key => $value) {
if(!is_array($value))
continue;
foreach($days as $dKey => $dValue) {
if($key != $dValue['a'])
continue;
if(isset($dValue['k']) && $dValue['k'] == $value['override'])
continue;
end($dValue);
$lastKey = key($dValue);
$dValue[$lastKey++] = $value['override'];
break;
}
}
print_r($days);
答案 1 :(得分:0)
<?php
foreach($holidays AS $hkey=>$hval){
if(empty($hval))
continue;
foreach ($days as $day) {
if($day['a'] != $hkey)
continue;
if(!is_array($hval))
continue;
end($day);
$lastKey = key($day);
$newLastKey = chr(ord($lastKey)+1);
$day[$newLastKey] = $hval['override'];
break;
}
}
此代码未经过测试....如果您遇到问题请尝试让我知道我会尝试