-1

How do I change an array into a string uning php?

Here's the array

{
    "result": "success", 
    "message": [ 
        { 
            "date_insert": "2017-01-28 20:14:51", 
            "date_update": "2017-01-28 20:15:11", 
            "weather": "sunny"
        }
    ]
}

I want the weather output in string . Thanks

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Zeeeeth
  • 11
  • 2
  • 1
    That's not an array. That's a json object. You need to show us how you get that info. If you get it as json, it probably already is a string. Either way, look into [`json_encode()`](http://php.net/manual/en/function.json-encode.php) and [`json_decode()`](http://php.net/manual/en/function.json-decode.php) – M. Eriksson Jan 28 '17 at 13:43

1 Answers1

2

First covert your json into array using json_decode() with second argument true.Then access each array element.

<?php

$json = '{
    "result": "success", 
    "message": [ 
        { 
            "date_insert": "2017-01-28 20:14:51", 
            "date_update": "2017-01-28 20:15:11", 
            "weather": "sunny"
        }
    ]
}';

$array = json_decode($json,true);//Now you have an array
//print_r($array);
echo $array['message'][0]['weather'];//outputs sunny

?>

For more see docs http://php.net/manual/en/function.json-decode.php

Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19