gpt4 book ai didi

php - 如何构造嵌套关联数组

转载 作者:行者123 更新时间:2023-12-05 08:17:40 24 4
gpt4 key购买 nike

我正在尝试构造一个深度嵌套的关联数组,但我不知道构造一个的规则是什么。我有这个理论数组:

-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

所以我的问题是,写这样一个数组的正确方法是什么?当我想添加更多 child 时应该遵循什么规则?

注意:在我的理论数组中,四有四个 child ,九有三个 child ,三有六个 child 。无论如何,我希望在我的虚拟数组中理解生 child 的想法。

最佳答案

子数组是顶级数组元素的实际值,您必须使用 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'
),
),
);

请注意,我还在每次关闭 ) 之后添加了 ,,因为正如我所说,数组是父数组元素的值。

关于php - 如何构造嵌套关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com