Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a nested associative array

Tags:

php

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.

like image 826
Gandalf Avatar asked Jul 18 '26 04:07

Gandalf


2 Answers

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.

like image 98
Jeroen Avatar answered Jul 19 '26 20:07

Jeroen


$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);
like image 26
Brian Avatar answered Jul 19 '26 22:07

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!