duanjie6912 2019-01-24 14:16
浏览 81
已采纳

使用PHP Curl查找JSON的匹配值? [重复]

This question already has an answer here:

So I've got a page with some jSON data I want to gather into arrays with cURL. To be precise, cURL when used from a PHP program, not from the command line.

I can do that part well with the following codeblock below:

$token = 'my_token_here';
$headers = ['Authorization: Bearer ' . $token];
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_RETURNTRANSFER => true,
  CURLINFO_HEADER_OUT => true,
  CURLOPT_URL => 'https://my.infrastructure.com/api/v1/accounts/search',
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_SSL_VERIFYPEER => true,
  CURLOPT_HTTPHEADER => $headers
]);

$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp);
dd($data);

This will return me a 2D array of all the accounts on the page in the above URL, each array containing an 'account' with variables like ID, name, etc.

The printed output, should I run "print_r($data)", is this:

  Array ( [0] => stdClass Object ( [id] => 130375 [name] => 3Lakes [domain] => 3lakes.instructure.com [distance] => [authentication_provider] => google ) [1] => stdClass Object ( [id] => 130376 [name] => 3Lakes - Parents [domain] => 3lakes.instructure.com [distance] => [authentication_provider] => canvas ) [2] => stdClass Object ( [id] => 126425 [name] => 95 Percent Group [domain] => 95percentgroup.instructure.com [distance] => [authentication_provider] => ) [3] => stdClass Object ( [id] => 129409 [name] => AACM [domain] => aacm.instructure.com [distance] => [authentication_provider] => canvas ) [4] => stdClass Object ( [id] => 129272 [name] => Aalen [domain] => aalen.instructure.com [distance] => [authentication_provider] => canvas ) [5] => stdClass Object ( [id] => 129572 [name] => Aaron Cohn Middle School - MCSD [domain] => mcsd.instructure.com [distance] => [authentication_provider] => ldap ) [6] => stdClass Object ( [id] => 128124 [name] => Abilene Christian University [domain] => acu.instructure.com [distance] => [authentication_provider] => ) [7] => stdClass Object ( [id] => 127204 [name] => Abilene Christian University Online [domain] => acuonline.instructure.com [distance] => [authentication_provider] => ) [8] => stdClass Object ( [id] => 130398 [name] => Abington Public Schools [domain] => abington.instructure.com [distance] => [authentication_provider] => canvas ) [9] => stdClass Object ( [id] => 128797 [name] => Academia Virtual [domain] => canvas.academiavirtual.cr [distance] => [authentication_provider] => ) )

Now, instead of doing all that, I just want to collect a single array, where that array has an ID equal to an ID I give it; i.e if I want to find the ID "15", it returns either the array matching that, or null.

I am not sure how, though. I know that, after the query, I can do PHP to comb the retrieved $data and check each, but I want to do this a bit smoother, presumably in the curl_setopt_array area somehow.

Is what I'm trying to do possible in this way? Any advice?

</div>
  • 写回答

1条回答 默认 最新

  • dongmeijian1716 2019-01-24 14:24
    关注

    Your confusing some things here. cURL has no effect on the data format that is transported because its a library for networking. You already got the solution, looping over the array and restructure the data to your needs. Also you dont have a 2D array but an array of stdClass objects.

    So first of all if you want to have an 2d array you should pass true as the 2nd argument to json_decode to make it parse JSON-objects into associative arrays instead of stdClass. Then you use a foreach loop and map your entries by id:

    $mappedData = [];
    foreach ($data as $item) {
      $mappedData[$item->id] = $item; //Or $mappedData[$item["id"]] if assoc
    }
    

    There may be some fancy callback methods using array_map etc. but the foreach loop is the simplest (in my opinion).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?