3

I am trying to construct a deeply nested associative array but i don't know what the rules are in constructing one.I have this theoritical array:

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

and i made this attempt at representing that as a php associative array;

$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];

When i try executing the php snippet i get this error;

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

So my question is,what is the correct way of writing such an array and what rule should i follow when i wish to add more children?.

Note:In my theoritical array,four has four children,nine has three children and three has six children.Anyway,i hope the idea of having children is understood in my dummy array.

0

2 Answers 2

16

The subarrays are actual values of your top-level array elements, and you have to initiate them using array() as well:

$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'
        ),
    ),
);

Note that I also added ,s after each closing ), because, like I said, the arrays are values of the parent array elements.

Sign up to request clarification or add additional context in comments.

Comments

2
$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.