我正试图构造一个深嵌套的关联数组,但我不知道构建关联数组的规则是什么。
-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中的语法错误,意外的'(',期望‘)
因此,我的问题是,怎样才是正确的写法?当我希望增加更多的子女时,我应遵循甚麽规则?
注意:在我的理论数组中,四有四子,九有三子,三有六children.Anyway,我希望在我的虚拟数组中理解有孩子的想法。
发布于 2012-07-04 12:44:07
子数组是顶级数组元素的实际值,您还必须使用数组()启动它们:
$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'
),
),
);注意,我还在每个关闭的,之后添加了),因为正如我所说的,数组是父数组元素的值。
发布于 2012-07-04 12:47:56
$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);https://stackoverflow.com/questions/11329338
复制相似问题