我正在尝试构建一个深度嵌套的关联数组,但我不知道构造一个规则是什么。我有这个理论数组:
-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代码段时,我收到此错误;
解析错误:语法错误,意外'(',期待')' 第7行的C:\ wamp \ www \ array.php
所以我的问题是,编写这样一个数组的正确方法是什么?当我希望添加更多孩子时,我应该遵循什么规则?
注意:在我的理论数组中,四个有四个孩子,九个有三个孩子,三个有六个孩子。无论如何,我希望在我的虚拟阵列中理解生孩子的想法。
答案 0 :(得分:10)
子数组是顶级数组元素的实际值,您还必须使用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'
),
),
);
请注意,我在每次关闭,
后都添加了)
,因为,就像我说的那样,数组是父数组元素的值。
答案 1 :(得分: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);