2015-09-03 68 views
1

我已閱讀過很多關於SO的答案,但我無法弄清楚如何讓它們適用於我的問題。如何將元素插入到嵌套的PHP關聯數組中?

這是我有:

{ 
    "name": "My Company LLC ->", 
    "children": [ 
     { 
      "name": "District of the Stores", 
      "children": [ 
       { 
        "name": "johnny1" 
       }, 
       { 
        "name": "jonny2" 
       } 
      ] 
     }, //I don't want my array to end here 
     { 
      "name": "store number 10", 
      "children": [ 
       { 
        "name": "johnny3" 
       }, 
       { 
        "name": "jonny4" 
       } 
      ] 
     } 
    ] 
} 

這就是我想要的。

{ 
    "name": "My Company LLC ->", 
    "children": [ 
     { 
      "name": "District of the Stores", 
      "children": [ 
       { 
        "name": "johnny1" 
       }, 
       { 
        "name": "jonny2" 
       },   
       { 
        "name": "store number 10", 
        "children": [ 
         { 
          "name": "johnny3" 
         }, 
         { 
          "name": "jonny4" 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

這是我試圖做到這一點的:

$name=array('name'=>'My Company LLC ->'); 
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2')))); 
$name['children'][]=array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'jonny4'))); 
    echo '<pre>'; 
    echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); 
    echo '</pre>'; 

我知道我的孩子陣列的這是造成在第一個例子我的第二個數組的問題最終將一個數組,但我不知道如何將第二個兒童陣列放回原來的第一個。這是來自數據庫的大列表的一部分,但我在這裏卡住了。我看不到我找到的SO答案幫助我插入第二個數組。

+0

'$名稱[ '孩子'] [] =陣列(...)'。你在錯誤的地方放置了「10號店」。您可能需要'$ name ['children'] [0] ['children'] [] = array(...)'。 –

+0

我認爲如果您打印出數組本身而不是將其編碼爲json,那麼您可以更容易地對這類內容進行可視化和調試。像這樣:echo'

'; print_r($name); echo '
'; –

回答

2

它看起來像你想的:

$name['children'][0]['children'][] = array(...); 

這將引用根元素(指數0)的第一個孩子,然後繼續像你加入到該元素children陣列以前一樣。

+0

謝謝。我想我會在未來看到遞歸。 – johnny

+0

@johnny:這看起來確實如此,因爲你似乎在這裏使用樹結構。 – jwueller

1
$name=array('name'=>'My Company LLC ->'); 
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2'), array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4')))))); 
    echo '<pre>'; 
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); 
echo '</pre>'; 

這裏是輸出

{ 
    "name": "My Company LLC ->", 
    "children": [ 
     { 
      "name": "District of the Stores", 
      "children": [ 
       { 
        "name": "johnny1" 
       }, 
       { 
        "name": "jonny2" 
       }, 
       { 
        "name": "store number 10", 
        "children": [ 
         { 
          "name": "johnny3" 
         }, 
         { 
          "name": "johnny4" 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

如果你想保留的代碼行分開的,你可以做到以下幾點:

$name=array('name'=>'My Company LLC ->'); 
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2')))); 
$name['children'][0]['children'][] = array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'johnny4'))); 
    echo '<pre>'; 
echo json_encode($name, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); 
echo '</pre>'; 

而且,它可能是更容易了很多通過查看數組而不是編碼到json來調試這類事情。這裏是你的原始輸出作爲一個數組,它真的向你展示了什麼更好。

$name=array('name'=>'My Company LLC ->'); 
$name['children']=array(array('name'=>'District of the Stores', 'children'=>array(array('name'=>'johnny1'), array('name'=>'jonny2')))); 
$name['children'][]=array('name'=>'store number 10', 'children'=>array(array('name'=>'johnny3'), array('name'=>'jonny4'))); 
echo '<pre>'; 
print_r($name); 
echo '</pre>'; 

,輸出是:

Array 
(
    [name] => My Company LLC -> 
    [children] => Array 
     (
      [0] => Array 
       (
        [name] => District of the Stores 
        [children] => Array 
         (
          [0] => Array 
           (
            [name] => johnny1 
           ) 

          [1] => Array 
           (
            [name] => jonny2 
           ) 

         ) 

       ) 

      [1] => Array 
       (
        [name] => store number 10 
        [children] => Array 
         (
          [0] => Array 
           (
            [name] => johnny3 
           ) 

          [1] => Array 
           (
            [name] => jonny4 
           ) 

         ) 

       ) 

     ) 

)