0

I'm learning how to make nested arrays, following the json response that I made.

1
  • call db_paket_id() inside db_paket Commented Dec 19, 2019 at 12:40

3 Answers 3

1

You can use array_merge to merge two or more arrays.
In your case:

$res = [
    'data' => array_merge(
        $this->M_wilayah->db_paket($origin,$destinasi),
        ['harga' => $this->M_wilayah->db_paket_id($origin,$destinasi)]
    )
];
Sign up to request clarification or add additional context in comments.

Comments

0

There's array_merge function to combine arrays, then you can make an array of array:

$res = [
    'data' => array_merge(
        $this->M_wilayah->db_paket($origin, $destinasi),
        ['harga' => $this->M_wilayah->db_paket_id($origin, $destinasi)]
 )
];

And you must have an json as follow:

[
    { "data": {
            "id_origin": "1",
            "origin": "jakarta raya",
            "id_destinasi": "7",
            "destinasi": "DKI Jakarta, Kota Jakarta Pusat, Tanah Abang, Bendungan Hilir, 10210",
            "harga": {
                "reguler": "8500",
                "nextday": "15000"
            }
        }    
    },
    { "data": {
            "id_origin": "2",
            "origin": "another origin",
            "id_destinasi": "8",
            "destinasi": " another data like DKI Jakarta, Kota Jakarta Pusat, Tanah Abang, Bendungan Hilir, 10210",
            "harga": {
                "reguler": "8500",
                "nextday": "15000"
            }
        }
    }
]

Comments

0

use db_paket_id in db_paket methods or

$res['data']['harga'] = $this->M_wilayah->db_paket_id($origin, $destinasi);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.