28 Oct

Generate JSON from PHP

$data = array(
    (object)array(
        'nama' => 'Andi Taru',
        'alamat' => 'Salatiga',
    ),
    (object)array(
        'nama' => 'Niken',
        'alamat' => 'Ungaran',
    ),
);
echo json_encode($data);

Hasil dari kode program di atas adalah:

[{"nama":"Andi Taru","alamat":"Salatiga"},{"nama":"Niken","alamat":"Ungaran"}]

artinya ada dua data di dalam JSON, setiap data memiliki nama dan alamat.

Leave a Reply

Your email address will not be published. Required fields are marked *