In Action

Php Toycode - Array of Arrays

Simple example demonstrating how to use a hash to store arrays.

Php in Action


forloop



Toycode

Php Code

Just cut-n-paste to reuse.

     
<?php
$akey="a1";
$akey2="a2";
$iarr = array("a","b","c");
$iarr2 = array("x","y","y");
//hash storing an array
$aarr[$akey] = $iarr;
$aarr[$akey2] = $iarr2;

//loop through assoc array
foreach( $aarr as $key => $value)
{
	echo "$key";
	echo " => $value";
	echo "<br \>";
}

	echo "<br \>-----------------------<br \>";

foreach( $aarr as $key => $value)
{
	echo "$key";
	echo " => $value";
	echo "<br \>";
	foreach( $value as $key2 => $value2)
  {
	 echo "$key2";
	 echo " => $value2";
	 echo "<br \>";
  }
}

echo "<br \>-----------------------<br \>";
echo $aarr["a1"][2] . "<br \>";

$sz = sizeof($iarr);
echo "<br \>-----------------------<br \>";
echo $sz . "<br \>";

//add element to array
$aarr["a1"][3] = 'd';
echo "<br \>-----------------------<br \>";
echo $aarr["a1"][3];

?>