PHP Array Access
From w3cyberlearnings
Contents |
PHP Access Array
Access array element using foreach() loop.
Note
Check for() loop and PHP Array Functions for additional detail on how to work with array.
Example 1
<?php $actor = array( 'Greta Garbo', 'Andy Garcia', 'Ava Gardner', 'Lucy Johnson' ); foreach($actor as $a) { echo $a; echo "<br/>"; } ?>
Output
Greta Garbo Andy Garcia Ava Gardner Lucy Johnson
Example 2
<?php $actor = array( 'id100' => 'Greta Garbo', 'id200' => 'Andy Garcia', 'id300' => 'Ava Gardner', 'id400' => 'Lucy Johnson' ); foreach($actor as $id=>$name) { echo "Id: {$id} Name: {$name}"; echo "<br/>"; } ?>
Output
Id: id100 Name: Greta Garbo Id: id200 Name: Andy Garcia Id: id300 Name: Ava Gardner Id: id400 Name: Lucy Johnson
Example 3
<?php $actor = array( 'id100' => array('name' => 'Greta Garbo', 'age' => 38), 'id200' => array('name' => 'Andy Garcia', 'age' => 31), 'id300' => array('name' => 'Ava Gardner', 'age' => 32), 'id400' => array('name' => 'Lucy Johnson', 'age' => 28) ); foreach ($actor as $id => $person) { echo "Identification: {$id}<br/>"; foreach ($person as $id => $infor) { echo "{$id}: {$infor}<br/>"; } echo "<br/>"; } ?>
Output
Identification: id100 name: Greta Garbo age: 38 Identification: id200 name: Andy Garcia age: 31 Identification: id300 name: Ava Gardner age: 32 Identification: id400 name: Lucy Johnson age: 28
Related Links
--PHP Array Introduction-- PHP Access Array-- PHP Array Convert to Object-- PHP Array Convert to JSON-- PHP Array Convert to Class-- PHP Array Convert to String--