How to write php returns json format according to the key? -


i use xampp create local networks , write php file return data in database json. code:

 <?php      $servername = "localhost";     $username = "root";     $password = "";     $dbname = "landslide";      // create connection     $conn = new mysqli($servername, $username, $password, $dbname);     if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error);         }   function myexample {     $mysqli = "select id, temp, acc, moisture, battery, time devices";     $result = $conn->query($mysqli);      if (mysqli_num_rows($result) > 0) {              while($row = mysqli_fetch_array($result)){             $response["main"] =array();             $response["parameters"]= array();             $main = array();             $main["id"]=$row["id"];             array_push($response["main"],$main);              $parameter = array();             $parameter["temp"] = $row["temp"];             $parameter["acc"] = $row["acc"];             $parameter["moisture"] = $row["moisture"];             $parameter["battery"] = $row["battery"];             $parameter["time"] = $row["time"];             array_push($response["parameters"],$parameter);          }             // echoing json response           $result_response =  echo json_encode($response);           return $result_response;           } } // end of example function  ?> 

now when call function json_encode format

now parse using

$res = json.parse($result_response); 

now

$moisture = $res['moisture']; 

my local link: http://127.0.0.1/landslide/currentdata.php .

now, want write 1 php file returns json format according "key"(here want key id). of openweather api address below, key cities (example london). http://api.openweathermap.org/data/2.5/weather?q=london,uk

so, how return json format php according key? please me! (my expression not good, sorry that)

when returning json_encode($response);

you need parse in object , access can achieive in 1 variable.

$result = json.parse($response); 

and access $result array if want access moisture

then $moisture = $result['moisture']; on...


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -