0

大量の json ファイルを取得し、それらのファイル内の情報からリンクを作成して、再帰的にファイルにアクセスする必要があります。

{
  "_v" : "12.2",
  "categories" : [ {
    "id" : "boys-hats-and-beanies",
    "name" : "Hats & Beanies"
    }
  ]
}

そのため、別のURLを作成して、ファイルの内容を取得する必要があります

http://xxx.xxx/?id=boys-hats-and=beanies.json

そのファイル内で、もう一度やり直す必要があるかもしれません。私が今持っているように、必要な情報を多くの配列に入れます。階層を維持したいと思います。

$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);

だから、私が何をしようとしているのかを見ることができます。どんな助けでも素晴らしいでしょう!

4

1 に答える 1

0

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;
}
于 2012-04-27T02:22:01.680 に答える