0
array(
    "bullony" => "one",
    "mcwhat" => "two",
    "daniweb" => "yay"
)

How to use this data to say for example
bullony has variable one or
mcwhat has variable two or
daniweb has variable yay.

How do I get name of array listed?
I know I could use print_p(); but it displays all.
I need only name of first two arrays. So bullony and mcwhat.

Edited by RikTelner

2
Contributors
2
Replies
12
Views
3 Years
Discussion Span
Last Post by RikTelner
1

Use a foreach loop to get the keys and the values:

foreach($array as $key => $value)
{
    echo "$key has variable $value";
}

You can also use array_keys() and array_values(), for example:

$akeys = array_keys($array);
$avalues = array_values($array);

for($i = 0; $i < count($array); $i++)
{
    echo "{$akeys[$i]} has variable {$avalues[$i]}";
}

Another approach with next(), current() and key():

for($i = 0; $i < count($array); $i++)
{
    echo key($array) ." has variable ". current($array) . "<br />";
    next($array);
}

Reference: http://php.net/manual/en/ref.array.php

Edited by cereal

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.