首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何构造嵌套关联数组

如何构造嵌套关联数组
EN

Stack Overflow用户
提问于 2012-07-04 12:41:25
回答 2查看 11.2K关注 0票数 2

我正试图构造一个深嵌套的关联数组,但我不知道构建关联数组的规则是什么。

代码语言:javascript
复制
-one
-two
-three
-four
    -one
    -two
    -three
    -four
-five
-six
-seven
-eight
-nine
    -one
    -two            
    -three 
            -one
            -two
            -three
            -four
            -five
            -six

我尝试将它表示为一个php关联数组;

代码语言:javascript
复制
$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,我希望在我的虚拟数组中理解有孩子的想法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-04 12:44:07

子数组是顶级数组元素的实际值,您还必须使用数组()启动它们:

代码语言:javascript
复制
$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'
        ),
    ),
);

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

票数 14
EN

Stack Overflow用户

发布于 2012-07-04 12:47:56

代码语言:javascript
复制
$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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11329338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档