Accessing Arrays inside Arrays In PHP
Accessing Arrays inside Arrays In PHP, the code below will show you how to access an Array inside an Array using a for loop.
(1) An Array named $robot is being created .
(2) Inside every array index of $robot , contains another Array.
(3) To print the Array inside . the programmed will first loop through the main Array $robot. while in the main Array $robot it will loop through the child Array .
Use for loop to Print out
<!DOCTYPE html> <html> <body> <?php $robot[0]=array(1,"bBot","1111"); $robot[1]=array(2,"cBot","2222"); $robot[2]=array(3,"dBot","3333"); $rlength= sizeof($robot); for ($x = 0; $x < $rlength; $x++) { for ($y = 0; $y <sizeof($robot[$x]); $y++) { echo $robot[$x][$y].","; } echo " "; }
Output
1,bBot,1111, 2,cBot,2222, 3,dBot,3333,
Use var_dump to Print out
Use var_dump to print out readable code for human
<?php $robot[0]=array(1,"bBot","1111"); $robot[1]=array(2,"cBot","2222"); $robot[2]=array(3,"dBot","3333"); foreach ($robot as $item){ echo ' <pre>'; var_dump($item); }
Output
array(3) { [0]=> int(1) [1]=> string(4) "bBot" [2]=> string(4) "1111" } array(3) { [0]=> int(2) [1]=> string(4) "cBot" [2]=> string(4) "2222" } array(3) { [0]=> int(3) [1]=> string(4) "dBot" [2]=> string(4) "3333" }
Use print_r to Print out
<!DOCTYPE html> <html> <body> <?php $robot[0]=array(1,"bBot","1111"); $robot[1]=array(2,"cBot","2222"); $robot[2]=array(3,"dBot","3333"); print " <pre>"; print_r($robot); print "</pre> "; ?> </body> </html>
Output
Array ( [0] => Array ( [0] => 1 [1] => bBot [2] => 1111 ) [1] => Array ( [0] => 2 [1] => cBot [2] => 2222 ) [2] => Array ( [0] => 3 [1] => dBot [2] => 3333 ) )
Check out Multidimensional Array Here
Leave a Reply