php 将 cURL json 数组响应转换为关联数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17396389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:49:31  来源:igfitidea点击:

turn cURL json array response to an associate array

phpjsoncurl

提问by vinylDeveloper

I have a cURL request like so

我有一个像这样的 cURL 请求

$ch = curl_init();
$data = 'filter=year(StartTime)' . urlencode(' eq 2013 and ') .'month(StartTime)'. urlencode(' eq 06') ;
curl_setopt($ch, CURLOPT_URL, "http://url.com/id()/events?$".$data);
$headers = array(
    'Accept: application/json',
    'Content-type: application/json',
    'X-ApiKey : XXXXXXXXXX'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, false);

$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);

?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>


</head>

<body>

<br><br>
<?php 
 echo "the name". $result['Name'];
?>

</body>
</html>

This is what it prints.

这是它打印的内容。

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 218 Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET- WEBSRV X-Powered-By: ARR/2.5 X-Powered-By: ASP.NET - ARR02 Date: Mon, 01 Jul 2013 02:52:31 GMT [{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}] 

the name

How can I put this into a Associative array?

我怎样才能把它放到一个关联数组中?

I've tried this

我试过这个

json_decode($response,true));

and this

和这个

ksort($response);

and this

和这个

var_dump($response);

and nothing seems to work..

似乎没有任何效果..

I want to be able to output like this

我希望能够像这样输出

echo $reponse['Name'];

Any help? Thanks

有什么帮助吗?谢谢

回答by msturdy

json_decodegives you an associative array when you pass "true" as the second argument:

json_decode您将“true”作为第二个参数传递时,为您提供一个关联数组:

  $json = '[{"Id":1079,"Name":"Test ","Status":1,"MemberId":1308,"Description":"This is a Test Event","SponsoredBy":null,"StartTime":"2013-06-30T12:00:00","EndTime":"2013-06-30T23:59:00","SearchDescription":null,"Types":[1,4,6]}]';

  $response = json_decode($json, true);

  echo $response[0]["Name"];

gives:

给出:

Test

Edit:

编辑:

json_decode()is giving you back an array of arrays, so you need to reference the array that is at position [0]in the response, if you get me.

json_decode()正在给你一个数组数组,所以你需要引用[0]响应中位置的数组,如果你得到我的话。

I've done that above in my example with $response[0], but have a look at this example, hope it makes it clearer!

我在上面的例子中用 $response[0] 做了这个,但是看看这个例子,希望它更清楚!

  $result = json_decode($json, true);
  var_dump($result);

gives:

给出:

array(1) {
  [0]=>
  array(10) {
    ["Id"]=>
    int(1079)
    ["Name"]=>
    string(5) "Test "
    ["Status"]=>
    int(1)
    ["MemberId"]=>
    int(1308)
    ["Description"]=>
    string(20) "This is a Test Event"
    ["SponsoredBy"]=>
    NULL
    ["StartTime"]=>
    string(19) "2013-06-30T12:00:00"
    ["EndTime"]=>
    string(19) "2013-06-30T23:59:00"
    ["SearchDescription"]=>
    NULL
    ["Types"]=>
    array(3) {
      [0]=>
      int(1)
      [1]=>
      int(4)
      [2]=>
      int(6)
    }
  }
}

then.. to access the array itself:

然后..访问数组本身:

  $result = json_decode($json, true);

  $result = $result[0]; // let's just reassign this to get the array we want      
  var_dump($result);

gives:

给出:

array(10) {
  ["Id"]=>
  int(1079)
  ["Name"]=>
  string(5) "Test "
  ["Status"]=>
  int(1)
  ["MemberId"]=>
  int(1308)
  ["Description"]=>
  string(20) "This is a Test Event"
  ["SponsoredBy"]=>
  NULL
  ["StartTime"]=>
  string(19) "2013-06-30T12:00:00"
  ["EndTime"]=>
  string(19) "2013-06-30T23:59:00"
  ["SearchDescription"]=>
  NULL
  ["Types"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(4)
    [2]=>
    int(6)
  }
}

And now you can access the various elements of the array directly:

现在您可以直接访问数组的各种元素:

  $result = json_decode($json, true);
  $result = $result[0];

  echo "Name: ". $result["Name"] . "\nID:   " . $result["Id"] . "\nDescription: " . $result["Description"] . "\n";

now we get back:

现在我们回来了:

Name: Test 
ID:   1079
Description: This is a Test Event

hope that makes sense!

希望这是有道理的!

回答by Mihai

By default curl_exec outputs the data it gets from the server to the standard output so your $response variable doesn't really have the actual response data in it. If you want to get the data in a variable set the CURLOPT_RETURNTRANSFER option before calling curl_exec.

默认情况下 curl_exec 会将它从服务器获取的数据输出到标准输出,因此您的 $response 变量中并没有真正的响应数据。如果要在变量中获取数据,请在调用 curl_exec 之前设置 CURLOPT_RETURNTRANSFER 选项。

curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE)