Echo PHP Nested Array Contents + Keys

PHP LogoI’ve always had some trouble with the syntax for accessing Nested Arrays and their Keys in PHP so I wanted to make note of this.

Structure is as follows:

[sourcecode language=”php”]print_r $contents;[/sourcecode]
([Cutbacks.jpg] => Array
(
[time] => 1207637110
[hash] => 05dee882129fe1574bcc8379ad3a40ac
[size] => 43928
)
[matrix3.zip] => Array
(
[time] => 1208040588
[hash] => 5f317404880304b82e9502fb399f2737
[size] => 46790
)
)
Code:

[sourcecode language=”php”]
foreach( $contents as $key => $list ) {
echo ‘Filename: ‘ . $key . "\n";
echo ‘Time: ‘ . $list[‘time’] . "\n";
echo ‘Hash: ‘ . $list[‘hash’] . "\n";
echo ‘Size: ‘ . $list[‘size’] . "\n";
echo ‘—-Next File—-‘ . "\n";
}[/sourcecode]

Outputs:

Filename: Cutbacks.jpg
Time: 1207637110Hash: 05dee882129fe1574bcc8379ad3a40ac
Size: 43928
----Next File----
Filename: matrix3.zip
Time: 1208040588
Hash: 5f317404880304b82e9502fb399f2737
Size: 46790
----Next File----

Leave a Reply

Your email address will not be published. Required fields are marked *