如何从 PHP 中的多个 JSON 数组中获取一个对象?
How can I get an object out of more than one JSON array in PHP?
我是 Whosebug 的新手,如果我的格式不正确,我深表歉意。我正在使用 GitHub API,我的目标是在下拉列表中获取用户的存储库列表,他们可以从中 select。
假设存储库列表 URL 是 https://api.github.com/users/MY_GITHUB_USERNAME/repos(按照我的安排,我可以通过 $userdata->repos_url
获得存储库 URL)。当我使用以下内容时:
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, $userdata->repos_url);
curl_setopt($curl1, CURLOPT_HEADER, 0);
curl_setopt($curl1, CURLOPT_HTTPHEADER, array(
'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth'
));
curl_setopt($curl1,CURLOPT_USERAGENT,'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl1, CURLOPT_SSL_VERIFYHOST, 0);
$cont1 = curl_exec($curl1);
curl_close($cont1);
echo $cont1;
它响应如下:
[
{
(information I don't need)
"full_name": "github-username/this-is-what-i-want",
(information I don't need)
}
]
目前我只有一个存储库。我想要做的是制作一个仅回显 full_name
的代码,如果有多个数组则每个回显。 (所有数组都会有 full_name
。)
有人知道我该怎么做吗?
将其解码为一个数组,然后遍历数组直到得到你想要的:
$data=json_decode($cont1, true); <~~~ tells php to decode the JSON into an array
$results=$data['FirstArray']['SecondArray']['NumResultsReturned']; <~~ most JSON's have a value showing how many arrays got sent back to you in the results. You didn't give a true data example as a reply, so can't give exacts on these fields for you.
$i="0";
while ($i < $results) <~~ loop through the arrays.
{
(Do stuff with your arrays, either ignore if non-important or capture to variables / display if you want it).
++$i;
}
我是 Whosebug 的新手,如果我的格式不正确,我深表歉意。我正在使用 GitHub API,我的目标是在下拉列表中获取用户的存储库列表,他们可以从中 select。
假设存储库列表 URL 是 https://api.github.com/users/MY_GITHUB_USERNAME/repos(按照我的安排,我可以通过 $userdata->repos_url
获得存储库 URL)。当我使用以下内容时:
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL, $userdata->repos_url);
curl_setopt($curl1, CURLOPT_HEADER, 0);
curl_setopt($curl1, CURLOPT_HTTPHEADER, array(
'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth'
));
curl_setopt($curl1,CURLOPT_USERAGENT,'User-Agent: MY_WEBSITE_HERE Addon Developer OAuth');
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl1, CURLOPT_SSL_VERIFYHOST, 0);
$cont1 = curl_exec($curl1);
curl_close($cont1);
echo $cont1;
它响应如下:
[
{
(information I don't need)
"full_name": "github-username/this-is-what-i-want",
(information I don't need)
}
]
目前我只有一个存储库。我想要做的是制作一个仅回显 full_name
的代码,如果有多个数组则每个回显。 (所有数组都会有 full_name
。)
有人知道我该怎么做吗?
将其解码为一个数组,然后遍历数组直到得到你想要的:
$data=json_decode($cont1, true); <~~~ tells php to decode the JSON into an array
$results=$data['FirstArray']['SecondArray']['NumResultsReturned']; <~~ most JSON's have a value showing how many arrays got sent back to you in the results. You didn't give a true data example as a reply, so can't give exacts on these fields for you.
$i="0";
while ($i < $results) <~~ loop through the arrays.
{
(Do stuff with your arrays, either ignore if non-important or capture to variables / display if you want it).
++$i;
}