我有一个关联数组(名称 = $holidays),如下所示。每个键可能指向一个值,也可能不指向一个值。如果一个键指向一个值,则该值是一个数组,如下所示。(键 [4] 指向具有键=> 值对 [override] => 1 的数组)
Array
(
[1] =>
[2] =>
[3] =>
[4] => Array
(
[override] => 1
)
[5] =>
[6] =>
[7] =>
)
我有另一个 2D 关联数组(名称 = $days),如下所示。
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] =>
)
)
我基本上需要像这样在 2 个数组之间进行比较:获取 $holidays 数组的第一个键(我们将其命名为 $val1)
然后转到 $days 数组,访问第一个子数组,获取该子数组的键 [a](我们将其命名为 $val2),然后检查
($val1 和 $val2) 是否相同以及
数组 $holiday 的键是否包含值(正如我在上面提到的 $holiday 数组的键可能不指向任何值),
如果这两个条件都为真,
则将新的 key=>value 对添加到 $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] =>
)
)
希望很清楚。任何人都可以帮忙吗?提前致谢。