0

i have json data like this decode from API, i get with PHP

{
  "0": {
    "id_siswa": "14477",
    "rombel": "15",
    "nama_lengkap": "Cahyo"
  },
  "1": {
    "id_siswa": "14484",
    "rombel": "15",
    "nama_lengkap": "Bowo"
  },
  "2": {
    "id_siswa": "14485",
    "rombel": "13",
    "nama_lengkap": "Agus Sugiharto"
  }
}

but when i call

$data[0]->id_siswa 

Uncaught Error: Cannot use object of type stdClass as array

then i try to call like

$data->id_siswa 

error : Undefined property: stdClass::$id_siswa in

Cedan Misquith
  • 1,134
  • 9
  • 20
Ricky Lhein
  • 15
  • 1
  • 5

2 Answers2

2

You need to decode json to get array value. you will get id_siswa output.

$json = '{
    "0": {
      "id_siswa": "14477",
      "rombel": "15",
      "nama_lengkap": "Cahyo"
    },
    "1": {
      "id_siswa": "14484",
      "rombel": "15",
      "nama_lengkap": "Bowo"
    },
    "2": {
      "id_siswa": "14485",
      "rombel": "13",
      "nama_lengkap": "Agus Sugiharto"
    }
 }';
 echo "<pre>";
$result = json_decode($json,true);
print_r($result);
echo $result[0]['id_siswa'];
Vaibhavi S.
  • 1,083
  • 7
  • 20
0

you can use json_decode to convert json into array and get the corresponding values with $data[0]['id_siswa'] like this.

$result = json_decode($json,true);
Amit Sharma
  • 1,775
  • 3
  • 11
  • 20