Php 来自多个JSON文件的多级数组
我必须拉一堆json文件,从这些文件中的信息创建链接,然后递归地进入这些文件Php 来自多个JSON文件的多级数组,php,arrays,json,multidimensional-array,Php,Arrays,Json,Multidimensional Array,我必须拉一堆json文件,从这些文件中的信息创建链接,然后递归地进入这些文件 { "_v" : "12.2", "categories" : [ { "id" : "boys-hats-and-beanies", "name" : "Hats & Beanies" } ] } 因此,我需要构建另一个url以进入并获取文件内容 在那个文件里,我可能得再做一次。正如我现在所拥有的,它将我需要的信息放入许多数组中,我希望它保留层次结构 $allLinks
{
"_v" : "12.2",
"categories" : [ {
"id" : "boys-hats-and-beanies",
"name" : "Hats & Beanies"
}
]
}
因此,我需要构建另一个url以进入并获取文件内容
在那个文件里,我可能得再做一次。正如我现在所拥有的,它将我需要的信息放入许多数组中,我希望它保留层次结构
$allLinks['root'] = array();
$allLinks['firstLevel'] = array();
$allLinks['secondLevel'] = array();
$allLinks['thirdLevel'] = array();
function getContent($info){
$content = file_get_contents($info);
return json_decode($content, true);
}
$new = getContent('https://xxx.xxx/?id=root');
foreach ($new['categories'] as $name => $value) {
array_push($allLinks['root'], 'https://xxx.xxx/?id='.$value['id']);
}
foreach ($allLinks['root'] as $name => $value) {
$new = getContent($value);
foreach ($new['categories'] as $name => $value) {
array_push($allLinks['firstLevel'], 'https://xxx.xxx/?id='.$value['id']);
}
}
foreach ($allLinks['firstLevel'] as $name => $value) {
$new = getContent($value);
foreach ($new['categories'] as $name => $value) {
array_push($allLinks['secondLevel'], 'https://xxx.xxx/?id='.$value['id']);
}
}
foreach ($allLinks['secondLevel'] as $name => $value) {
$new = getContent($value);
foreach ($new['categories'] as $name => $value) {
array_push($allLinks['thirdLevel'], 'https://xxx.xxx/?id='.$value['id']);
}
}
print_r($allLinks);
所以你可以看到我想表达的意思。任何帮助都会很好 似乎您试图将URL存储在一个数组中,这将返回多维数组中的所有URL,其中0是第一级
function getContent($info){
$content = file_get_contents($info);
return json_decode($content, true);
}
function getContentUrlById($id, $ext = '.json')
{
return 'https://xxx.xxx/?id=' . $id . $ext;
}
function getContentRecursive($id = 'root', $level = 0)
{
$result = array();
$url = getContentUrlById($id);
$content = getContent($url);
$result[$level][] = $url;
foreach($content['categories'] as $cat){
$result = array_merge_recursive($result, getContentRecursive($cat['id'], $level + 1));
}
return $result;
}
给我这个
数组([0]=>数组([0]=>https://xxx.xxx?id=root )[1]=>数组([0]=>https://xxx.xxx/?id=mens )
等等。。