Jump to content

Arrays with named keys.


daydreamer

Recommended Posts

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

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));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.