Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Location
    The Netherlands
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple array problem

    Hey, I had this problem with arrays (can't remember what it was now) which went correct when using array(), but failed when using square-bracket syntax. It was something like this:

    PHP Code:
    $arr['first level']['second level'][1] = 'bla';
    $arr['first level']['second level'][2]['11oneone'] = 'foo';
    $arr['first level']['second level'][2]['bar'] = false
    Why doesn't that work. and does the following?

    PHP Code:
    $arr = array(
        
    'first level' => array(
            
    'second level' => array(
                
    => 'bla',
                
    => array(
                    
    '11oneone' => 'foo',
                    
    'bar' => false
                
    )
            )
        ); 
    Note that this was by heart, so typos may occur :P
    Yeah that.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You must create an array before storing a key-value pair in it, e.g:
    PHP Code:
    $arr = array();
    $arr['first level'] = array();
    $arr['first level']['second level'] = array();
    $arr['first level']['second level'][1] = 'bla';
    $arr['first level']['second level'][2] = array();
    $arr['first level']['second level'][2]['11oneone'] = 'foo'
    $arr['first level']['second level'][2]['bar'] = false
    dumpfi


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •