--------------------------------------------------- | Date: 2003-03-16 14:19:13 | | Filename: ass_array.php | | Author: detour@metalshell.com | | | | http://www.metalshell.com/ | --------------------------------------------------- <?php /* ass_array.php by detour@metalshell.com * * Example on using associative arrays. * * http://www.metalshell.com/ * */ /** Assigning Values **/ // One at a time. $ass_array["black"] = "white"; $ass_array["up"] = "down"; // Multiple using array() $ass_array = array("black" => "white", "up" => "down", "left" => "right", "night" => "day"); /** Displaying the Values **/ echo('<H2>Display values individually</H2>'); echo($ass_array["black"] . ', ' . $ass_array["up"] . '<br><br>'); echo('<H2>Display all values.</H2>'); foreach($ass_array as $key => $value) echo($key . ' = ' . $value . '<br>'); /* You can also sort the array by either the key or value. * * asort() - sort in ascending order * arsort() = sort in descending order * ksort() = sort by the key in ascending order * */ asort($ass_array); // You can also cycle through the array by using a pointer // Use reset() to set the pointer at the beginning of the array reset($ass_array); $num = count($ass_array); echo('<H2>Display all values using a pointer. Note they are sorted now.</H2>'); for($x = 0; $x < $num; $x++) { echo(key($ass_array) . ' = ' . current($ass_array) . '<br>'); // Increment the pointer next($ass_array); } ?>