2012-07-04 59 views
1

我試圖構建一個深層嵌套的關聯數組,但我不知道規則是什麼構建one.I有這個theoritical陣列:如何構建一個嵌套的關聯數組

-one 
-two 
-three 
-four 
    -one 
    -two 
    -three 
    -four 
-five 
-six 
-seven 
-eight 
-nine 
    -one 
    -two    
    -three 
      -one 
      -two 
      -three 
      -four 
      -five 
      -six 

和我做了這種嘗試將它表示爲一個php關聯數組;

$associative = array(
'one' => 'one-1', 
'two' => 'two-2', 
'three' => 'three-3', 
'four' => 'four-4' 
(
    'one' => 'one-four-1', 
    'two' => 'two-four-2', 
    'three' => 'three-four-3', 
    'four' => 'four-four-4' 
) 
'five' => 'five-5', 
'six' => 'six-6', 
'seven' => 'seven-7', 
'eight' => 'eight-8', 
'nine' => 'nine-9' 
(
    'one' => 'one-nine-1', 
    'two' => 'two-nine-2',   
    'three' => 'three-nine-3' 
( 
     'one' => 'one-nine-three-1', 
     'two' => 'two-nine-three-2', 
     'three' => 'three-nine-three-3', 
     'four' => 'four-nine-three-4', 
     'five' => 'five-nine-three-5', 
     'six' => 'six-nine-three-6' 
)) 
); 
$keys = array_values($associative); 
echo $keys[0]; 

當我嘗試執行php代碼片段時出現此錯誤;

Parse error: syntax error, unexpected '(', expecting ')' in C:\wamp\www\array.php on line 7

所以我的問題是,什麼是寫這樣的一個數組的正確方式,我應該遵循什麼樣的規則,當我要添加更多的孩子?

注意:在我的理論陣列中,四個有四個孩子,九個有三個孩子,三個有六個孩子。無論如何,我希望在我的虛擬陣列中瞭解生孩子的想法。

回答

9

子陣列是頂層數組元素的實際值,你必須使用array()以及啓動它們:

$associative = array(
    'one' => 'one-1', 
    'two' => 'two-2', 
    'three' => 'three-3', 
    'four' => array(
     'one' => 'one-four-1', 
     'two' => 'two-four-2', 
     'three' => 'three-four-3', 
     'four' => 'four-four-4' 
    ), 
    'five' => 'five-5', 
    'six' => 'six-6', 
    'seven' => 'seven-7', 
    'eight' => 'eight-8', 
    'nine' => array(
     'one' => 'one-nine-1', 
     'two' => 'two-nine-2',   
     'three' => array( 
      'one' => 'one-nine-three-1', 
      'two' => 'two-nine-three-2', 
      'three' => 'three-nine-three-3', 
      'four' => 'four-nine-three-4', 
      'five' => 'five-nine-three-5', 
      'six' => 'six-nine-three-6' 
     ), 
    ), 
); 

注意,每個閉合)之後,我還添加了, S,因爲像我說過,數組是父數組元素的值。

+0

唉打我吧。 – Brian

1
$associative = array(
    'one' => 'one-1', 
    'two' => 'two-2', 
'three' => 'three-3', 
'four' => array(
    'one' => 'one-four-1', 
    'two' => 'two-four-2', 
    'three' => 'three-four-3', 
    'four' => 'four-four-4' 
), 
    'five' => 'five-5', 
    'six' => 'six-6', 
    'seven' => 'seven-7', 
    'eight' => 'eight-8', 
    'nine' => array(
    'one' => 'one-nine-1', 
    'two' => 'two-nine-2',   
    'three' => array(
     'one' => 'one-nine-three-1', 
     'two' => 'two-nine-three-2', 
     'three' => 'three-nine-three-3', 
     'four' => 'four-nine-three-4', 
     'five' => 'five-nine-three-5', 
     'six' => 'six-nine-three-6' 
    ) 
) 
); 
print_r($associative);