mercredi 23 juin 2021

Convert JSON IN LARAVEL

Here is my JSON

[
    {
        "TIMESTAMP": "2021-06-09 13:13:26",
        "COL1": "10",
        "COL2": "20",
        "COL3": "30"
    },
    {
        "TIMESTAMP": "2021-06-22 13:13:26",
        "COL1": "20",
        "COL2": "30",
        "COL3": "40"
    },
    {
        "TIMESTAMP": "2021-06-21 13:13:26",
        "COL1": "1",
        "COL2": "2",
        "COL3": "3"
    },
    {
        "TIMESTAMP": "2021-06-20 13:13:26",
        "COL1": "40",
        "COL2": "50",
        "COL3": "60"
    }
]   

I need to refactor the json According to the Column name like (EXPECTED OUTPUT)

[
    {
      "TITLE":"COL1"
      "DATA":[10,20,1,40]
      
    },
    {
       "TITLE":"COL2"
      "DATA":[20,30,2,50]
      
    },
    {
      "TITLE":"COL3"
      "DATA":[30,40,3,60]
      
    },
]
  

I was tried but it not working

$data = json_decode($result, true); 
$refactored = array_map(function($item) {
    return (object)[
        'TIMESTAMP' => $item['TIMESTAMP'],
        'DATA' => [ $item['COL1'], $item['COL2'], $item['COL3'] ]
    ];
}, $data);
dump($refactored);

Someone help me out with this. The column may be 3 or more and it must be dynamic. Thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3qrjWxB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire