在php中用数字索引重建数组

时间:2018-02-23 06:54:25

标签: php

我有一个

形式的PHP数组$data
[
    0 => [
        'id' => 3,
        'month' => '2018-03',
        'dataA' => 5,
    ],
    1 => [
         'id' => 4
         'month' => '2018-04',
         'dataA' => 3,
         'dataB' => 2,
    ],
]

id是月份编号,dataAdataB可能存在于每个数组中,但可能不存在。

我必须从第1个月到第12个月重建它,以便它就像

[
    0 => [
       'month' => '2018-01',
       'dataA' => 0,
       'dataB' => 0,
    ],
    1 => [
       'month' => '2018-02',
       'dataA' => 0,
       'dataB' => 0,
    ],
    2 => [
       'month' => '2018-03',
       'dataA' => 5,
       'dataB' => 0,
    ],
    3 => [
       'month' => '2018-04',
       'dataA' => 3,
       'dataB' => 2,
    ],
    ....
]

我必须为每个数组元素添加dataAdataB,如果不存在则将值设置为0,将月份设置为1到12。

我正在尝试使用for循环作为

for ($i = 1; $i<=12; $i++) {
   $key = array_search($i, $data);

   print_r($key);
}

但它打印匹配值的key,就像它为dataA返回$i = 3一样。

如何检查$i数组中是否存在id $data

2 个答案:

答案 0 :(得分:1)

只需从1到12循环,然后使用array_searcharray_column

获取密钥
$data = array(
    array(
        'id' => 3,
        'month' => '2018-03',
        'dataA' => 5,
    ),
    array(
         'id' => 4,
         'month' => '2018-04',
         'dataA' => 3,
         'dataB' => 2,
    ),
);

$newData = array();
for ( $i = 1; $i<=12; $i++ ) {
    $key = array_search($i , array_column($data, 'id'));

    $newData[] = array(
        'month' => "2018-" . str_pad($i, 2, "0", STR_PAD_LEFT),
        'dataA' => isset( $data[ $key ]["dataA"] ) && is_int( $key ) ? $data[ $key ]["dataA"] : 0,
        'dataB' => isset( $data[ $key ]["dataB"] ) && is_int( $key ) ? $data[ $key ]["dataB"] : 0,
    );
}

echo "<pre>";
print_r( $newData );
echo "</pre>";

这将导致:

Array
(
    [0] => Array
        (
            [month] => 2018-01
            [dataA] => 0
            [dataB] => 0
        )

    [1] => Array
        (
            [month] => 2018-02
            [dataA] => 0
            [dataB] => 0
        )

    [2] => Array
        (
            [month] => 2018-03
            [dataA] => 5
            [dataB] => 0
        )

    [3] => Array
        (
            [month] => 2018-04
            [dataA] => 3
            [dataB] => 2
        )

    [4] => Array
        (
            [month] => 2018-05
            [dataA] => 0
            [dataB] => 0
        )

    [5] => Array
        (
            [month] => 2018-06
            [dataA] => 0
            [dataB] => 0
        )

    [6] => Array
        (
            [month] => 2018-07
            [dataA] => 0
            [dataB] => 0
        )

    [7] => Array
        (
            [month] => 2018-08
            [dataA] => 0
            [dataB] => 0
        )

    [8] => Array
        (
            [month] => 2018-09
            [dataA] => 0
            [dataB] => 0
        )

    [9] => Array
        (
            [month] => 2018-10
            [dataA] => 0
            [dataB] => 0
        )

    [10] => Array
        (
            [month] => 2018-11
            [dataA] => 0
            [dataB] => 0
        )

    [11] => Array
        (
            [month] => 2018-12
            [dataA] => 0
            [dataB] => 0
        )

)

答案 1 :(得分:0)

使用array_reduce() + array_replace()函数的完整解决方案:

$arr = [
    0 => [
        'id' => 3,
        'month' => '2018-03',
        'dataA' => 5,
    ],
    1 => [
         'id' => 4,
         'month' => '2018-04',
         'dataA' => 3,
         'dataB' => 2,
    ],
];
$curr_months = array_reduce($arr, function($r, $a){
    unset($a['id']);
    $r[$a['month']] = $a;
    return $r;
}, []);

$m_pfx = '2018-';
$result = [];
foreach (range(1, 12) as $i) {
    $k = sprintf("%d-%02d", $m_pfx, $i);
    $curr_item = (isset($curr_months[$k]))? $curr_months[$k] : [];
    $item = array_replace(['month' => $k, 'dataA' => '', 'dataB' => ''], $curr_item);
    $result[] = $item;
}    

print_r($result);

输出:

Array
(
    [0] => Array
        (
            [month] => 2018-01
            [dataA] => 
            [dataB] => 
        )

    [1] => Array
        (
            [month] => 2018-02
            [dataA] => 
            [dataB] => 
        )

    [2] => Array
        (
            [month] => 2018-03
            [dataA] => 5
            [dataB] => 
        )

    [3] => Array
        (
            [month] => 2018-04
            [dataA] => 3
            [dataB] => 2
        )

    [4] => Array
        (
            [month] => 2018-05
            [dataA] => 
            [dataB] => 
        )

    [5] => Array
        (
            [month] => 2018-06
            [dataA] => 
            [dataB] => 
        )

    [6] => Array
        (
            [month] => 2018-07
            [dataA] => 
            [dataB] => 
        )

    [7] => Array
        (
            [month] => 2018-08
            [dataA] => 
            [dataB] => 
        )

    [8] => Array
        (
            [month] => 2018-09
            [dataA] => 
            [dataB] => 
        )

    [9] => Array
        (
            [month] => 2018-10
            [dataA] => 
            [dataB] => 
        )

    [10] => Array
        (
            [month] => 2018-11
            [dataA] => 
            [dataB] => 
        )

    [11] => Array
        (
            [month] => 2018-12
            [dataA] => 
            [dataB] => 
        )
)