Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with multi-level arrays

Tags:

arrays

php

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!

like image 934
Toussant Landowner Avatar asked Nov 03 '22 17:11

Toussant Landowner


1 Answers

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

like image 82
Michael Berkowski Avatar answered Nov 08 '22 03:11

Michael Berkowski