PHP填充数组空洞

我有一个数组,其中包含每年、每周存储的值,这些值来自我的数据库。结果可能如下所示:


Array

(

    [2018] => Array

        (

            [40] => 1

            [41] => 1

            [47] => 1

            [48] => 1

            [52] => 1

        )

    [2019] => Array

        (

            [1] => 1

            [2] => 1

            [3] => 1

            [4] => 1

        )

)



or



Array

(

    [2018] => Array

        (

            [48] => 1

            [49] => 1

        )

    [2019] => Array

        (

            [3] => 1

            [4] => 1

        )

)

如您所见,有几周没有数据。我想填满数组,所以所有星期都在那里。以最后一个数组作为演示,期望的结果是:


Array

(

    [2018] => Array

        (

            [40] => 1

            [41] => 1

            [42] => 0 < added

            [43] => 0 < added

            [44] => 0 < added

            [45] => 0 < added

            [46] => 0 < added

            [47] => 1

            [48] => 1

            [49] => 0 < added

            [50] => 0 < added

            [51] => 0 < added

            [52] => 1

        )

    [2019] => Array

        (

            [1] => 1

            [2] => 1

            [3] => 1

            [4] => 1

        )

)

Array

(

    [2018] => Array

        (

            [48] => 1

            [49] => 1

            [50] => 0 < added

            [51] => 0 < added

            [52] => 0 < added

        )

    [2019] => Array

        (

            [1] => 1

            [2] => 

            [3] => 1

            [4] => 1

        )

)

所以,数组应该:

  • 填补两个(不连续)数字之间的空洞

  • 如果有下一年,则填充年份数组直到结束

  • 如果有上一年且第一周没有数据,则填写新一年的第一周

我怎样才能做到这一点?


慕勒3428872
浏览 135回答 2
2回答

ABOUTYOU

我认为最有效的方法是使用联合运算符。这应该这样做:&nbsp; <?php&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; "2018"=> array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "40" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "41" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "47" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "48" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "52" => 1&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; "2019"=> array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6" => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "7" => 1&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );&nbsp; &nbsp; foreach($data as $year=>$array){&nbsp; &nbsp; &nbsp; &nbsp; $keys = array_keys($array);&nbsp; &nbsp; &nbsp; &nbsp; $min = min($keys); $max = 52;&nbsp; &nbsp; &nbsp; &nbsp; if(!isset($data[$year+1])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $max = max($keys);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $data[$year] = $data[$year] + array_fill($min,$max-$min+1, 0);&nbsp; &nbsp; &nbsp; &nbsp; ksort($data[$year]);&nbsp; &nbsp; }&nbsp; &nbsp; print_r($data);&nbsp; ?>输出:Array(&nbsp; &nbsp; [2018] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [40] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [41] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [42] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [43] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [44] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [45] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [46] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [47] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [48] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [49] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [50] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [51] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [52] => 1&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2019] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [6] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [7] => 1&nbsp; &nbsp; &nbsp; &nbsp; ))

慕盖茨4494581

您可以使用一组数组函数,例如array_fill()和array_replace()来实现这一点。解释见评论$array = [&nbsp; &nbsp; '2018' => [&nbsp; &nbsp; &nbsp; &nbsp; '40' => 1,&nbsp; &nbsp; &nbsp; &nbsp; '41' => 1,&nbsp; &nbsp; &nbsp; &nbsp; '47' => 1,&nbsp; &nbsp; &nbsp; &nbsp; '48' => 1,&nbsp; &nbsp; &nbsp; &nbsp; '52' => 1&nbsp; &nbsp; ],&nbsp; &nbsp; '2019' => [&nbsp; &nbsp; &nbsp; &nbsp; '3' => 1,&nbsp; &nbsp; &nbsp; &nbsp; '4' => 1&nbsp; &nbsp; ],];$result = []; // initialize result array$years = array_keys($array); // Get given years$first_year = min($years);&nbsp; &nbsp;// Get first year$last_year = max($years);&nbsp; &nbsp; // Get last yearforeach ($array as $year => $value) { // Loop thru each year array&nbsp; &nbsp; $weeks = array_keys($value); // Get weeks&nbsp; &nbsp; $start = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set first week&nbsp; &nbsp; $weeknum = 52;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Set number of weeks&nbsp; &nbsp; if ($year === $first_year) { // If first year, change number of weeks&nbsp; &nbsp; &nbsp; &nbsp; $start = min($weeks);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $weeknum -= $start - 1;&nbsp; // number of weeks is 52 - the first week number - 1&nbsp; &nbsp; }&nbsp; &nbsp; if ($year === $last_year) {&nbsp; // If last year, change number of weeks to last given week&nbsp; &nbsp; &nbsp; &nbsp; $weeknum = max($weeks);&nbsp; // Weeks will be 1 - last given week&nbsp; &nbsp; }&nbsp; &nbsp; $result[$year] = array_replace(array_fill($start, $weeknum, 0), $value);}var_dump($result);这样做的第一个目标是填充基于年份的数组。如果是第一年,则创建第一个给定周到第 52 周的数组如果是去年,则创建第 1 周到最后一周的数组如果在中间的某个地方,请创建第 1 周到第 52 周的数组然后用相同的键替换现有的数据数组
打开App,查看更多内容
随时随地看视频慕课网APP