daydreamer Posted September 6, 2008 Share Posted September 6, 2008 So i need to create an array with named keys. E.g $theVariable = array("google" => "http//google.com", "yahoo"=> "http://yahoo.com"); The information is coming from a database, so when I create the array I wont know the key names, but i need to print them on page, with the corresponding values. How would I print a list like this: Key Value google http//google.com yahoo http://yahoo.com etc So basically how do I retrieve the names of the keys without knowing what they are, and how could I echo them to the page? Obviously i know how to access the values: $theVariable[keyname]; Thanks. Link to comment https://forums.phpfreaks.com/topic/123003-arrays-with-named-keys/ Share on other sites More sharing options...
BlueSkyIS Posted September 6, 2008 Share Posted September 6, 2008 array_keys -- Return all the keys of an array Description array array_keys ( array input [, mixed search_value [, bool strict]] ) array_keys() returns the keys, numeric and string, from the input array. If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned. As of PHP 5, you can use strict parameter for comparison including type (===). Example 1. array_keys() example <?php $array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array)); ?> Link to comment https://forums.phpfreaks.com/topic/123003-arrays-with-named-keys/#findComment-635152 Share on other sites More sharing options...
BlueSkyIS Posted September 6, 2008 Share Posted September 6, 2008 or simply use a foreach loop: foreach ($some_array AS $key=>$value) { echo "key: $key, value: $value<br>"; } Link to comment https://forums.phpfreaks.com/topic/123003-arrays-with-named-keys/#findComment-635154 Share on other sites More sharing options...
daydreamer Posted September 6, 2008 Author Share Posted September 6, 2008 thanks, the foreach loop looks like what i need. perfect Link to comment https://forums.phpfreaks.com/topic/123003-arrays-with-named-keys/#findComment-635158 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.