Gagantous Gagantous - 29 days ago 8
JSON Question

How to delete parent array JSON string and leave an child array JSON string?

I have a JSON array like this ( it has an array inside an array like the code below )

{
"data": [
{
"id": "40",
"memo_kondisi": "OKe lah",
"id_kondisi": "010118",
"total_row": "2",
"nilai_temuan": "10.000.000",
"nilai_potensi_kerugian": "9.000.000",
"data_sebab": [
{
"id": "12",
"id_sebab": "0202",
"total_row_b": "2",
"memo_sebab": "sebab 1",
"data_rekomendasi": [
{
"id": "14",
"id_rekomendasi": "",
"nilai_rekomendasi": "0",
"memo_rekomendasi": "",
"data_tindak_lanjut": [
{
"id": "20",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
},
{
"id": "21",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
}
]
},
{
"id": "14",
"id_rekomendasi": "",
"nilai_rekomendasi": "0",
"memo_rekomendasi": "",
"data_tindak_lanjut": [
{
"id": "20",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
},
{
"id": "21",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
}
]
}
]
}
]
}
]
}


How can I make the json array to look like this ( the array parrent will be removed and will leave a child array ( it's like push array but its reverse))
Based on id ( Or smothing besides the id or you can said not to be specific but will delete the parent array of JSON) ?

{
"data": [
{
"id": "40",
"memo_kondisi": "OKe lah",
"id_kondisi": "010118",
"total_row": "2",
"nilai_temuan": "10.000.000",
"nilai_potensi_kerugian": "9.000.000",
"data_sebab": [{
"id": "20",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
},
{
"id": "21",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
},
{
"id": "20",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
},
{
"id": "21",
"id_tindak_lanjut": "",
"nilai_tindak_lanjut": "0",
"memo_tindak_lanjut": "",
"tindak_lanjut_no": "1",
"tgl_tindak_lanjut": "0000-00-00"
}
]
}
]
}

Answer Source

You've got 4 nested arrays, so in order to flatten the middle two tiers, you'll have to execute 4 nested loops.

Demo: https://3v4l.org/XWpZB

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

$result = [];

foreach ($data as $datum) {
    // copy top tier
    $copy = $datum;
    // clear children
    $copy['data_sebab'] = null;

    foreach ($datum['data_sebab'] as $sebab) {
        foreach ($sebab['data_rekomendasi'] as $rekomendasi) {
            foreach ($rekomendasi['data_tindak_lanjut'] as $tindak_lanjut) {
                // copy leaf child
                $copy['data_sebab'] []= $tindak_lanjut;
            }
        }
    }

    // nest last leaf under top tier
    $result['data'] []= $copy;
}

echo json_encode($result, JSON_PRETTY_PRINT);