Jump to content

Get keys and values from an array


HerrPNut

Recommended Posts

Hellow,

 

I have an array $arr with keys and values, I can display the values if i define a key:

 

<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12];    // 1
?>

 

but i want to display all the keys from the array

In this example this would be: foo and 12

Link to comment
https://forums.phpfreaks.com/topic/192616-get-keys-and-values-from-an-array/
Share on other sites

Hellow,

 

I have an array $Data with keys and values. With a foreach I can display all the values:

foreach($Data as $var)
{
    echo $var;
}

 

but I also want to display al the keys... how can i do that?

 

Quite easily, change your foreach() line to this:

 

foreach($Data as $key => $var)

 

That will allow you to use $key.

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.