5

hello i am learning PHP and came upon this multi-level array after using print_r on $this->root

 Array ( 
    [0] => 9 
    [obj] => 3562 
    [gen] => 0 
    [1] => Array ( 
        [0] => 5 
        [1] => Array ( 
            [/AcroForm] => Array ( 
                [0] => 8 
                [1] => 3563 
                [2] => 0 
                ) 
            [/Metadata] => Array ( 
                [0] => 8 
                [1] => 3559 
                [2] => 0 
                ) 
            [/PageLabels] => Array ( 
                [0] => 8 
                [1] => 3389 
                [2] => 0 
                ) 
            [/Pages] => Array ( 
                [0] => 8 
                [1] => 3392 
                [2] => 0 
                ) 
            [/Type] => Array ( 
                [0] => 2 
                [1] => /Catalog 
                ) 
            ) 
        ) 
    ) Array ( 
        [0] => 9 
        [obj] => 8 
        [gen] => 0 
        [1] => Array ( 
            [0] => 5 
            [1] => Array ( 
                [/Type] => Array ( 
                    [0] => 2 
                    [1] => /Catalog 
                    ) 
                [/Pages] => Array ( 
                    [0] => 8 
                    [1] => 1 
                    [2] => 0 
                    ) 
                [/OpenAction] => Array ( 
                    [0] => 6 
                    [1] => Array ( 
                        [0] => Array ( 
                            [0] => 8 
                            [1] => 3 
                            [2] => 0 
                            ) 
                        [1] => Array ( 
                            [0] => 2 
                            [1] => /FitH 
                            ) 
                        [2] => Array ( 
                            [0] => 0 
                            ) 
                        ) 
                    ) 
                [/PageLayout] => Array ( 
                    [0] => 2 
                    [1] => /OneColumn 
                    ) 
                ) 
            ) 
        ) 

i have an question about the behavior of using multi-level arrays, i want to use this function

$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);

and $this->root[1][1]['/Pages'] which i believe is used to check the array for these keys and if it exists then use as variable for pdf_resolve_object

so my question is 2-fold, one is does $this->root[1][1]['/Pages'] check the array and goes through the keys? if not what is its behavior? and 2 when it checks the array does it go through just the top 4 keys or all of the sub-keys?

If someone can help or link me to some learning material that would be much appreciated, thank you!

1
  • if i am completely wrong please inform me!! Commented Aug 23, 2012 at 1:00

1 Answer 1

2

1) It does not check for the presence of the array keys -- rather it assumes that those keys already exist and passes the value into the function. If any of the keys did not exist, PHP would issue an E_NOTICE to the effect of Notice: Undefined index: that the key was not found. To check for them would require a call to isset() or array_key_exists() like:

if (isset($this->root[1][1]['/Pages'])) {
  $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
}

2) There is no need for it to iterate through to find the keys. Knowing the array keys already means they can be accessed directly without iteration. In memory, PHP has stored the array keys and the memory locations of the values they point to. Therefore, with the key alone, PHP can return the value without needing to traverse the array.

There is a lot of good information in the PHP manaul on Arrays

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

2 Comments

do you mean an Undefined offset? @Michael Berkowski
@ToussantLandowner A Nonexsitent numeric index like [9] will issue Undefined offset and a nonexistent string key like ['somekey'] will issue Undefined index.

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.