Json array bersarang pada codeigniter

Saya sudah mencoba untuk memforeach di bagian array bersarangnya namun dibagian id_penjualan2 data penjualandetail id_penjualan1 kembali terulang, mohon bantuannya. terimakasih sekolahkoding

function index_get()
    {
       $id_penjualan = $this->get('id_penjualan');
       if ($id_penjualan == '')
       {
        $datux = $this->datapenjualan_model->listing();
        $data = array();
        $data2 = array();
        foreach ($datux as $key => $result)
        {
          $datex = $this->datapenjualan_model->listingdetil($result->id_penjualan);
          foreach ($datex as $key => $dapat) {
                $data2[] = array(
                  'item' => $dapat->item,
                  'qty' => $dapat->qty,
                  'harga_satuan' => $dapat->harga_satuan,
                  'subtotal' => $dapat->subtotal
                );
          }
            $data[] = array(
                'id_penjualan' => $result->id_penjualan,
                'nama_pelanggan' => $result->nama_pelanggan,
                'tanggal' => $result->tanggal,
                'jam' => $result->jam,
                'total' => $result->total,
                'bayar_tunai'=> $result->bayar_tunai,
                'kembali'=> $result->kembali,
                'pembelian_detil' => $data2
            );
        }
    }

{
        "id_penjualan": "1",
        "nama_pelanggan": "Aris Abdul Ajis",
        "tanggal": "2019-09-24",
        "jam": "29:05:06",
        "total": "5000",
        "bayar_tunai": "50000",
        "kembali": "45000",
        "pembelian_detil": [
            {
                "item": "Donut",
                "qty": "10",
                "harga_satuan": "5000",
                "subtotal": "50000"
            },
            {
                "item": "Cokelat",
                "qty": "2",
                "harga_satuan": "5000",
                "subtotal": "10000"
            }
        ]
    },

{
        "id_penjualan": "2",
        "nama_pelanggan": "Syifa Malinda Dewi",
        "tanggal": "2019-09-24",
        "jam": "22:13:32",
        "total": "10000",
        "bayar_tunai": "30000",
        "kembali": "20000",
        "pembelian_detil": [
            {
                "item": "Donut",
                "qty": "10",
                "harga_satuan": "5000",
                "subtotal": "50000"
            },
            {
                "item": "Cokelat",
                "qty": "2",
                "harga_satuan": "5000",
                "subtotal": "10000"
            },
            {
                "item": "Handpohone",
                "qty": "1",
                "harga_satuan": "5000",
                "subtotal": "5000"
            },
            {
                "item": "Kue",
                "qty": "2",
                "harga_satuan": "7000",
                "subtotal": "14000"
            }
        ]
    },

biar tampilnya jadi begini :

{
        "id_penjualan": "1",
        "nama_pelanggan": "Aris Abdul Ajis",
        "tanggal": "2019-09-24",
        "jam": "29:05:06",
        "total": "5000",
        "bayar_tunai": "50000",
        "kembali": "45000",
        "pembelian_detil": [
            {
                "item": "Donut",
                "qty": "10",
                "harga_satuan": "5000",
                "subtotal": "50000"
            },
            {
                "item": "Cokelat",
                "qty": "2",
                "harga_satuan": "5000",
                "subtotal": "10000"
            }
        ]
    },
{
        "id_penjualan": "2",
        "nama_pelanggan": "Syifa Malinda Dewi",
        "tanggal": "2019-09-24",
        "jam": "22:13:32",
        "total": "10000",
        "bayar_tunai": "30000",
        "kembali": "20000",
        "pembelian_detil": [
            {
                "item": "Handpohone",
                "qty": "1",
                "harga_satuan": "5000",
                "subtotal": "5000"
            },
            {
                "item": "Kue",
                "qty": "2",
                "harga_satuan": "7000",
                "subtotal": "14000"
            }
        ]
    },

avatar aris026
@aris026

4 Kontribusi 0 Poin

Diperbarui 5 tahun yang lalu

1 Jawaban:

coba ini $data2, masukkan di foreach $datux <pre> foreach ($datux as $key =&gt; $result) { $data2 = array(); } </pre>

avatar dianarifr
@dianarifr

642 Kontribusi 316 Poin

Dipost 5 tahun yang lalu

Login untuk ikut Jawaban