In Action

Php Toycode - Hash of Hashes

This example shows how to tie two hashes together. It can take a bit of study on your part in order to get the concept.

Php in Action


forloop



Toycode

Php Code

Just cut-n-paste to reuse.

     
<?php
$aarr["1993-03-02"] = '1993-03-02';
$aarr["1992-11-30"] = '1992-11-30';
$aarr["1996-12-01"] = '1996-12-01';
$aarr["1992-01-22"] = '1992-01-22';
$aarr["1992-12-14"] = '1992-12-14';
$aarr["1993-03-11"] = '1993-03-11';

$sz = sizeof($aarr);
echo "size of \$aarr array is " . $sz . "<br />";

$labdisptest = "CHEM ROUTINE";
$iarr=array();
//add outer hash elem
$iarr[$labdisptest] = "";

//add another outer
$labdisptest = "CBC";
$iarr[$labdisptest] = "";
//add another outer ... look ma no dups
$labdisptest2 = "CHEM ROUTINE";
$iarr[$labdisptest2] = "";
//check out outer stash...
echo "<br \>-----------------------<br \>";
 foreach( $iarr as $key => $value)
 {
 	echo "$key";
 	echo " => $value";
 	echo "<br \>";
 }

$sz = sizeof($iarr);
echo "<br \>-----------------------<br \>";
echo "size of array is " . $sz . "<br />";

//assign our inner hask values to an outer hash
$iarr["CHEM ROUTINE"] = $aarr;
//retrieve and inner hash element
echo "<br \>-----------------------<br \>";
echo "Retrieve elem in hash inside hash... " . 
$iarr["CHEM ROUTINE"]["1992-11-30"] . "<br />";

//retrieve all inner hash elements and convert to conventional
//array and sort
$sz2 = sizeof($iarr["CHEM ROUTINE"]);

echo "<br \>-----------------------<br \>";
echo "size of array \$iarr[\"CHEM ROUTINE\"] is " . $sz2 . "<br />";
rsort($iarr["CHEM ROUTINE"]);

echo "<br \>-----------------------<br \>";
for ($i=0;$i<$sz2;$i++)
{
	echo $iarr["CHEM ROUTINE"][$i] . "<br />";
}
?>