Home > Mobile >  Use subarray data from json to create rows of associative arrays
Use subarray data from json to create rows of associative arrays

Time:11-21

I want to map my JSON response to specific array construction.

Here is my JSON data in short:

"code": 200,
"unit": null,
"data": [
    {
        "2022-11-16": 185.6159202
    },
    {
        "2022-11-17": 204.31997631
    },...
]

I need help to map this data to have array structure just like look array below:

Array ( 
    [0] => Array(
        [date] => 2018-01-03
        [value] => 0.0002444
    )
[1] => Array(
        [date] => 2018-01-04
        [value] => 0.0171476
    )
)

My code in PHP:

$decoded = json_decode($json, true);
$arr = $decoded['data'];

After that, the structure of the array looks like this, which is not quite what I expect:

Array ( 
    [0] => Array ( [2018-01-03] => 0.0002444 ) 
    [1] => Array ( [2018-01-04] => 0.0171476 )
)

CodePudding user response:

Using array_keys() and array_values() is not an ideal/optimal approach because this generates unnecessary depth while accessing the data. See how [0] has to be written after each call? It is not best practice to create a temporary array if you are only going to access the first item from it. Just use key() and current() (or reset()).

Functional-style programming with array_map(): (Demo)

var_export(
    array_map(
        fn($item) => ['date' => key($item), 'value' => current($item)],
        json_decode($json, true)['data']
    )
);

Classic foreach(): (Demo)

$result = [];
foreach (json_decode($json, true)['data'] as $item) {
    $result[] = ['date' => key($item), 'value' => current($item)];
}
var_export($result);

Or with fewer iterated function calls: (Demo)

$result = [];
foreach (json_decode($json, true)['data'] as $item) {
    $key = key($item);
    $result[] = ['date' => $key, 'value' => $item[$key]];
}
var_export($result);

Or with compact(): (Demo)

$result = [];
foreach (json_decode($json, true)['data'] as $item) {
    foreach ($item as $date => $value) {
        $result[] = compact(['date', 'value']);
    }
}
var_export($result);

CodePudding user response:

foreach($decoded_array as $arr){
        $result[] = array('date' => array_keys($arr)[0], 'value' => array_values($arr)[0])

}

CodePudding user response:

I think this should work:

$array_input = json_decode($json, true)['data'];
$array_result = [];

foreach ($array_input as $i) {
    $array_result[] = [
        'date' => key($i), 
        'value' => current($i)
        ];
}
print_r($array_result);

  • Related