-3

I have json like this :

{"status":"TRUE","flag":1,"message":"Has Data","data":[{"country_id":"1","country_name":"India"}]}

This is the return value I am getting from api. I need to convert this into array and I need the values country_id and country_name.

I tried to convert using but unable to parse it as array

json_decode(); 

but still I am getting in std object format. Someone help me. Thanks in advance.

In model I am returning the values in

return $Result->result_array()
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Bergin
  • 188
  • 3
  • 12
  • 2
    Use `json_decode($json, true)` for an associative array. Read the docs. http://php.net/manual/en/function.json-decode.php – Emile Pels Jul 22 '18 at 08:01
  • Because it is an object. But I suppose, you need the 'data' member of it. – ZorgoZ Jul 22 '18 at 08:03
  • Possible duplicate of [How to convert JSON string to array](https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array) – Nigel Ren Jul 22 '18 at 08:06

1 Answers1

1

Use json_decode($json, true) ;

$json = '{"status":"TRUE","flag":1,"message":"Has Data","data":[{"country_id":"1","country_name":"India"}]}';
 $array = json_decode($json,TRUE);

echo $array['data'][0]['country_id'].PHP_EOL;
echo $array['data'][0]['country_name'];

See working demo : https://eval.in/1040313

Output

1
India
Pradeep
  • 9,667
  • 13
  • 27
  • 34