Tengo una matriz PHP $data con el formato

[
    0 => [
        'id' => 3,
        'month' => '2018-03',
        'dataA' => 5,
    ],
    1 => [
         'id' => 4
         'month' => '2018-04',
         'dataA' => 3,
         'dataB' => 2,
    ],
]

El id es el número del mes y dataA y dataB pueden existir en cada matriz y puede que no.

Tengo que reconstruirlo del mes 1 al 12 para que sea como

[
    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,
    ],
    ....
]

Tengo que agregar dataA y dataB a cada elemento de la matriz, si no existe, establezca el valor en 0 y el mes a partir del 1 al 12.

Estoy tratando de hacerlo usando for loop como

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

   print_r($key);
}

Pero imprime el key del valor coincidente como si devuelve dataA para $i = 3.

¿Cómo comprobar si $i th id existe en la matriz $data?

php
0
Anuj TBE 23 feb. 2018 a las 09:54

2 respuestas

La mejor respuesta

Simplemente repita del 1 al 12 y obtenga la clave usando array_search y array_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>";

Esto resultará en:

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
Eddie 23 feb. 2018 a las 07:20

Solución completa con funciones 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);

La salida:

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] => 
        )
)
0
RomanPerekhrest 23 feb. 2018 a las 07:28