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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.