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.

Recommended Answers

All 2 Replies

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

-

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.