1

我有一个关联数组(名称 = $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] => 
            )

    )

希望很清楚。任何人都可以帮忙吗?提前致谢。

4

2 回答 2

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);
于 2012-09-20T10:49:18.727 回答
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;
        }

    }

此代码未经测试....尝试如果您有问题然后让我知道我会尝试

于 2012-09-20T10:59:15.790 回答