2012-04-27 8 views
0

jsonファイルの束をプルして、それらのファイルの情報からリンクを作成し、再帰的にファイルに移動する必要があります。複数のJSONファイルからの無次元配列

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

ように、私は再びそれをしなければならないかもしれませんに行くと

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

とそのファイル内のファイルの内容を取得するには、別の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); 

私は何を得ようとしているのか分かります。助けて頂ければ幸いです。

答えて

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; 
} 
+0

'$のreturn'があるべき'私にこの 'アレイ ( を与えるreturn' – deex

+0

[0] =>アレイ ( [0] => https://xxx.xxx?id =ルート ) [1] =>配列 ( [0] => https://xxx.xxx/?id=mens ) ' そしてそうで.. –

関連する問題