我正在24小时的黑客马拉松试图解决这个问题,原谅如果它有点匆忙。
每个循环的第1个工作正常,我从这个网址获得了一个类别列表
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 ?}>
现在每个外观都有第二个,我想从这个网址中获取项目
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 ++ {&GT?; echo $ v?
foreach($json_b as $x){?>
if($v==$x):
echo $v
endif;
?>
}?>
答案 0 :(得分:2)
这将创建一个$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 ...
}
答案 1 :(得分:0)
体验JSON的数据结构与JSON类别不同,因此if($v==$x)
永远不会匹配。如果您想在类别网址中找到类别体验的所有结果,您可以执行以下操作:
<?
$BASE_URL = 'https://dev.xola.com/api/';
$categories = json_decode(file_get_contents($BASE_URL . 'categories'));
$experiences = json_decode(file_get_contents($BASE_URL . 'experiences'));
$matches = array();
foreach( $categories as $category ) {
foreach( $experiences->data as $experience ) {
if( $experience->category === $category ) {
$matches[] = $experience;
}
}
}
?>
<? foreach( $matches as $match ) : ?>
<? echo $match->category; ?><br>
<? endforeach; ?>