我正在24小时的骇客马拉松中尝试解决此问题,请原谅。
每个循环的第一个工作正常,我从此URL获取类别列表
https://dev.xola.com/api/categories
我用这个来抓清单
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
然后用这个循环
<?
foreach($json_a as $v)
{?>
echo $v ?}>
现在,每个外观都有第二个,我想从此URL中获取项目
https://dev.xola.com/api/experiences
与最后一个网址中的类别匹配
so samething
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_b = json_decode($string, true); //json decoder
这是我尝试过的完整循环
<?
$i=0;
foreach($json_a as $v)
$ i ++ {?> echo $ v?
foreach($json_b as $x){?>
if($v==$x):
echo $v
endif;
?>
}?>
这将创建一个$result
仅包含具有早期获取的类别的数据的数组:
<?php
$categories_url = "https://dev.xola.com/api/categories";
$data = file_get_contents($categories_url);
$categories = json_decode($data, true);
$experiences_url = "https://dev.xola.com/api/experiences";
$data = file_get_contents($experiences_url);
$experiences = json_decode($data, true);
$result = array();
foreach ($experiences['data'] as $experience)
{
if (in_array($experience['category'], $categories))
{
$result[] = $experience;
}
}
print_r($result);
而且,您可以使用以下命令轻松读取结果:
foreach ($result as $item)
{
echo $item['category'], "\n";
echo $item['desc'], "\n";
//... other data available ...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句