Skip to content

Instantly share code, notes, and snippets.

@r17x
Last active March 10, 2018 11:20
Show Gist options
  • Select an option

  • Save r17x/ef6d45d9e9706d51c9c120b9576e3aa1 to your computer and use it in GitHub Desktop.

Select an option

Save r17x/ef6d45d9e9706d51c9c120b9576e3aa1 to your computer and use it in GitHub Desktop.
Simple Arrray Map PHP
<?php
/**
* saya memiliki contoh JSON dalam bentuk string
*/
$stringJson = '{"message":[{"id":"23","description":"AXIS 5000","code":"AX5","code_alias":"AH5","substitute":"1","price":"5419","profit_max":"160","h2h":"5419","h2h_lock":"0","h2h_max":"100","price_host":"5319","nominal":"5000","operator":"AXIS","operator_sub":"AX","supplier":"lumbung","publish":"1","status":"normal","block":"0","date_update":"2016-01-26 13:35:02","provider":"AXIS","provider_sub":"REGULER"},{"id":"28","description":"AXIS 10000","code":"AX10","code_alias":"AH10","substitute":"1","price":"10419","profit_max":"160","h2h":"10419","h2h_lock":"0","h2h_max":"100","price_host":"10319","nominal":"10000","operator":"AXIS","operator_sub":"AX","supplier":"lumbung","publish":"1","status":"normal","block":"0","date_update":"2016-02-09 07:20:02","provider":"AXIS","provider_sub":"REGULER"}]}';
/**
* Disini Saya merubah dari format string Json ke
* PHP Object Menggunakan fungsi json_decode
*/
$dataToObject = json_decode($stringJson);
/**
* Saaya Deklarasi Anonymous Function untuk
* Membuat Array baru yang hanya berisi Nilai dari
* ID
*/
$filterID = function($x){
return $x->id;
};
/**
* Disini Proses Filter atau pengambilan dari variable yang
* yang sudah menampung object dari json string
* dengan menggunakan fungsi anonymous diatas
* di kerjakan oleh fungsi array_map
* di tampung di $dataToIdOnly
*/
$dataToIdOnly = array_map($filterID, $dataToObject->message);
echo json_encode($dataToIdOnly);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment