使用PHP Curl查找JSON的匹配值? [重复]
This question already has an answer here:
- PHP multidimensional array search by value 22 answers
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>
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).