数组检查日期是否存在于数组中,从句点日期范围填充上一个数组PHP的值

时间:2017-06-23 16:23:08

标签: php arrays

希望这篇文章不重复,我一直在寻找解决方案,但我真的陷入了困境,也许有人通过这个并且有解决方案。

我有一组传递到图表中的数组,即使在所需的日子里,一个子数组数据的值也不存在,欲望图需要显示所有数据。

这是我的阵列:

$setData = array(
            '2017-06-21' => array(
                'gender1' => array(
                    'value1' => 100,
                    'value2' => 100,
                    'value3' => 100,
                ),
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 300,
                    'value2' => 300,
                    'value3' => 300,
                )
            ),
            '2017-06-22' => array(
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ),
                'gender3' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                )
            ),
            '2017-06-23' => array(
                'gender2' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                ),
                'gender3' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                )
            ),
        );

所以,这样,图表中的日期22将会消除性别2的线条或条形图堆叠,性别1的23条线条或条形图堆叠,我需要的是保持线条或条形图堆叠,即使在22我不要&#39 ; t表示性别2的值,23表示性别1的值

期望的结果应该是:

$setDataNeed = array(
            '2017-06-21' => array(
                'gender1' => array(
                    'value1' => 100,
                    'value2' => 100,
                    'value3' => 100,
                ),
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 300,
                    'value2' => 300,
                    'value3' => 300,
                )
            ),
            '2017-06-22' => array(
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ), 
                /* Keep values from gender2 from 21 date*/
                'gender2' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                ),
                'gender3' => array(
                    'value1' => 200,
                    'value2' => 200,
                    'value3' => 200,
                )
            ),
            '2017-06-23' => array(
                /* Keep values for gender1 from 22 date*/
                'gender1' => array(
                    'value1' => 90,
                    'value2' => 90,
                    'value3' => 90,
                ),
                'gender2' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                ),
                'gender3' => array(
                    'value1' => 150,
                    'value2' => 150,
                    'value3' => 150,
                )
            ),
        );

2 个答案:

答案 0 :(得分:1)

只需记住上一行并在需要时插入即可。

function prepare($aIn) {
    $aRes = array();
    $prev = array();
    $keys = array('gender1', 'gender2', 'gender3',);

    foreach ($aIn as $row) {
        foreach ($keys as $k) {
            if (!isset($row[$k]) && isset($prev[$k])) {
                $row[$k] = $prev[$k];
            }
        }

        $aRes[] = $row;
        $prev = $row;
    }

    return $aRes;
}

答案 1 :(得分:0)

其他方式这样做。它保留了钥匙。

 $previousValue = null;
 foreach($setData as $key=>$value){;
     if($previousValue) {
        $setData[$key] = array_replace_recursive($previousValue,$value);
     }
     $previousValue = $value;    
 }
print_r($setData);