In Action

Php Toycode - Simple Associative Arrays

Simple associate array (hash) example. Not much else to say.

Php in Action


forloop



Toycode

Php Code

Just cut-n-paste to reuse.

     
<?php
$assocarray;
//add key and value
$assocarray["Php"] = "Php|4";
$assocarray["Perl"] = "Perl|2";
$assocarray["Sql"] = "Sql|3";

//change value
$assocarray["Php"] = "Php|1";

//add key hardcoding
$assocarray["C++"] = "";
//add key dynamically
$akey = "HTML";
$assocarray[$akey] = "";


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

//delete array item
unset($assocarray['C++']);
echo "<br />---------------\n<br />";
//quick check to see it is gone
print_r($assocarray);

//sort array
asort($assocarray);

echo "<br />---------------\n<br />";
foreach( $assocarray as $key => $value)
{
	echo "key is $key   ";
	echo "value is $value";
	echo "\n<br />";		
} 
?>