#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    What type of operand is an Array Index?


    Hi there,

    I got some time to revise programming (using a PHP book) and have come across a fundamental and probably simpler than I think question which answer I want to understand. I appreciate all feedback.

    The question is "What type of operand is an Array Index?".

    From what I know of an array, its index is the reference to the elements in the array. The book tells me the type of operand can be an integer, string or array. I know how the index can be an integer and string.

    For example,
    PHP Code:
    $alphabet = array('a'=>'aay''b'=>'bee''c'=>'see'4=>'four'
    is a case in which the index 'a' is a string and can also be referenced using the number 0
    PHP Code:
    $alphabet[0
    In what way or how can the index be an array? I will appreciate an example.

    Thanks!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Right after posting, I tried an example (by replacing one of the indices of the array with an array) that I thought might work, and it worked. So I realised how an Array Index can be of type array. I think I fully understand this now.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    Originally Posted by devphphreak
    Right after posting, I tried an example (by replacing one of the indices of the array with an array) that I thought might work, and it worked. So I realised how an Array Index can be of type array. I think I fully understand this now.
    No, it can't. I don't know what you tried but an array key cannot be an array. So
    PHP Code:
    array(array() => 123
    is invalid.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    I tried
    PHP Code:
    $alphabet = array('array(a)'=>'aay''b'=>'bee''c'=>'see'4=>'four'); 
    I am just realising my error. I made 'array(a)' a string instead of leaving it as an array. And when I tried using array(a) without the quotes, so it is a proper array, I got NULL after referencing it like so
    PHP Code:
    $alphabet[array('a')] 
    Which means this book is lying to me! And getting me 'foncused' about this. Arrgh. Or might it be that I do not understand the question? If I do understand the question, then it means an Array Index can only be a number or a string. Right?

    Thank you requinix for looking at this closely for me.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    PHP Code:
    $alphabet = array('a'=>'aay''b'=>'bee''c'=>'see'4=>'four'
    is a case in which the index 'a' is a string and can also be referenced using the number 0
    In that example,
    $alphabet['a'] will be 'aay'.
    $alphabet[0] is undefined, it does not reference 'aay'.

    An array index can be a string, a number, true/false or null. It cannot be an array or object.

    Array indexes are rarely referred to as operands (in any programming language, not just PHP), and doing so is sort stretching the meaning of the word.

    If the book's answer is a string or number then I can understand the question. However, I cannot think of any context involving array indexes in which an array, a string and a number can syntactically be used interchangeable (except as the right-hand operand in an assignment statement).
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Thanks E-Oreo! I found your post very insightful. And thank you for the correction as well. It shows me in an associative array, the indices specified are exactly that and their values cannot be referenced any other way except through the indices themselves.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Well, unfortunately, it's not quite that simply. The PHP developers had the genius idea of intermixing the concept of arrays (which are numerically indexed according to the order of the elements) and hashes (which have custom indices). This means you can have weird hybrids like this:
    PHP Code:
    $a = array('x' => 45'a' => 102); 
    The values 4 and 10 are indexed by 'x' and 'a' respectively. However, 5 and 2 are indexed automatically with 0 and 1 respectively.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Ah. Even more insightful! Weird way to intermix indexing, if you asked me. Thanks Jacques1!
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    array values can be arrays.

    Have fun!

    PHP Code:
    $a = array(
        array(
            
    'a',
            
    'b',
            
    'c' => 'C'
        
    ),
        
    'foo'=>array(1,2,3),
        array(
            
    'a','b','c'
        
    ),
        
    8,
        
    9,
        
    10 => array(),
        
    'bar'=>'baz',
        
    'qux'=>array(
            array(
                array(
                    
    'x'=>'y',
                    
    'z',
                    
    9,
                    
    8,
                    
    7
                
    )
            )
        )
    );

    $a['test'] = $a;

    print_r($a); 
    Code:
    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
                [c] => C
            )
    
        [foo] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
    
        [1] => Array
            (
                [0] => a
                [1] => b
                [2] => c
            )
    
        [2] => 8
        [3] => 9
        [10] => Array
            (
            )
    
        [bar] => baz
        [qux] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [x] => y
                                [0] => z
                                [1] => 9
                                [2] => 8
                                [3] => 7
                            )
    
                    )
    
            )
    
        [test] => Array
            (
                [0] => Array
                    (
                        [0] => a
                        [1] => b
                        [c] => C
                    )
    
                [foo] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                    )
    
                [1] => Array
                    (
                        [0] => a
                        [1] => b
                        [2] => c
                    )
    
                [2] => 8
                [3] => 9
                [10] => Array
                    (
                    )
    
                [bar] => baz
                [qux] => Array
                    (
                        [0] => Array
                            (
                                [0] => Array
                                    (
                                        [x] => y
                                        [0] => z
                                        [1] => 9
                                        [2] => 8
                                        [3] => 7
                                    )
    
                            )
    
                    )
    
            )
    
    )
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Thank you Northie. I think you have successfully changed the direction of the conversation. However, the information you provide is a useful reminder.

IMN logo majestic logo threadwatch logo seochat tools logo