array_values and array_keys

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

array_values and array_keys

Post by duk »

i have a array like this:

Code: Select all

<?

$array = array (
 
    '1' => 'value1',
    '2' => 'value2'
);

?>
if i want to echo the value2, im doing this

Code: Select all

$var = array_values($array);
echo $var[1];


its make me a real bit confuse how can i do a echo directly without first put the array in $var ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo $array['2'];
:?
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

lol i was realy confuse... thanks, i didn't know that php ignores the keys :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

you would be right with $array[1] to get the second value, except you renamed the keys starting with 1.. arrays start with 0
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply