如何将 api 请求的 json 数据存储到数组中


How do i store api requested json data into an array

我做了一个发出API请求的函数。(functions.php) 我已经将这个函数调用到一个名为 (results.php) 的文件中,该函数从搜索输入中选择 ID 从数据库中获取纬度和长度,我希望能够对返回的数据对每个执行操作。对于每个对象,将纬度和经度存储到数组中。将 json 数据粘贴到 json 查看器中,以便更好地查看它。整个事情从数据库连接到数据输出,我只是不知道如何将这些json数据存储到变量中,就像我说的那样,将每个对象的纬度和长度放入数组中

功能。.PHP

function locate($id, $connect) { //connect is coming from connection.php

 // Using prepared Statements means that SQL injection is not possible. Selects      ID from search input takes lat and long.
  if ($stmt = $connect->prepare("SELECT Lat, Lng FROM List WHERE ID = ? LIMIT 1")or die(mysql_error())) { 
  $stmt->bind_param('i', $id ); // Bind "$id" to parameter.
  $stmt->execute(); // Execute the prepared query.
  $stmt->store_result();
  $stmt->bind_result($lat, $lng); // get variables from result.
  $stmt->fetch();
  if($stmt->num_rows == 1) // If the id exists
  { 
    // Point to where you downloaded the phar
    include('httpful.phar');
    // Api request!
    $response = 'Httpful'Request::get('https://data.police.uk/api/crimes-street/all-crime?lat='.$lat.'&lng='.$lng)->send();
    echo $response;
  } 
  else
  {
     // No lat & long exists. 
     echo 'noUser';
     return false;
  }

}}

结果。.PHP

$id=$_POST["hiddenField"]; //ID equals the hidden field post made from the   search university form
locate($id, $connect); //function from functions.PHP

JSON 数据

[{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.629543","street":{"id":1199924,"name":"On or near Parking Area"},"longitude":"-0.744038"},"context":"","outcome_status":null,"persistent_id":"1504b407304004387c9ad979acce910041bab568af1e833045a6deb157c621ae","id":45880896,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.632071","street":{"id":1199912,"name":"On or near Bridge Street"},"longitude":"-0.756033"},"context":"","outcome_status":null,"persistent_id":"edc5f6be254e489548aad05aa5a92a3d1976fa668658940bb760aefb82788b0f","id":45882456,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.629583","street":{"id":1200038,"name":"On or near Bushey Close"},"longitude":"-0.773352"},"context":"","outcome_status":null,"persistent_id":"65c9fe3cb702f5bf7c147700262595e15ce1827d7484271dedef06444789447c","id":45880856,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.632071","street":{"id":1199912,"name":"On or near Bridge Street"},"longitude":"-0.756033"},"context":"","outcome_status":null,"persistent_id":"a604dbb1811a70e75709b01b492d19290a42fdaacaac1124f6c5b4a588b32d74","id":45882455,"location_subtype":"","month":"2015-12"},{"category":"anti-social-behaviour","location_type":"Force","location":{"latitude":"51.628932","street":{"id":1200020,"name":"On or near Sabina 

请参阅json_decode()

//Input (json) string/array
$string = '[{"Foo":"bar","123":456}]';
//Convert to PHP array
$array = json_decode($string, true):
//Output to check
print_r($array);

以上将输出:

Array
(
    [0] => Array
        (
            [Foo] => bar
            [123] => 456
        )
)