Php: how to get associative array key from numeric index?

You don't. Your array doesn't have a key 1 You could: Make a new array, which contains the keys: $newArray = array_keys($array); echo $newArray0 But the value "one" is at $newArray0 not 1 A shortcut would be: echo current(array_keys($array)) Get the first key of the array: reset($array); echo key($array) Get the key corresponding to the value "value": echo array_search('value', $array) This all depends on what it is exactly you want to do. The fact is 1 doesn't correspond to "one" any which way you turn it.

You don't. Your array doesn't have a key 1. You could: Make a new array, which contains the keys: $newArray = array_keys($array); echo $newArray0; But the value "one" is at $newArray0, not 1.

A shortcut would be: echo current(array_keys($array)); Get the first key of the array: reset($array); echo key($array); Get the key corresponding to the value "value": echo array_search('value', $array); This all depends on what it is exactly you want to do. The fact is, 1 doesn't correspond to "one" any which way you turn it.

$array = array( 'one' =>'value', 'two' => 'value2' ); $allKeys = array_keys($array); echo $allKeys0; (Result: ideone.com/pFh0o).

Aha! That's what I was looking for! – Ash Nov 4 '10 at 10:40.

Or if you need it in a loop foreach ($array as $key => $value) { echo $key . ':' . $value ."\n"; } //Result: //one:value //two:value2.

It echoing all keys. But topicstarter need to get key by numeric index. It is no the same.

– garvey Nov 4 '10 at 10:54 I'm well aware of that. But given the fact that topicstarter is new to php, this could have been what he was trying to achieve and just didn't know of the foreach syntax ;) – Decko Nov 12 '10 at 8:50.

$array = array( 'one' =>'value', 'two' => 'value2' ); $keys = array_keys($array); echo $keys0; // one echo $keys1; // two.

You might do it this way: function asoccArrayValueWithNumKey(&$arr, $key) { if (!(count($arr) > $key)) return false; reset($array); $aux = -1; $found = false; while (($auxKey = key($array)) &&! $found) { $aux++; $found = ($aux == $key); } if ($found) return $array$auxKey; else return false; } $val = asoccArrayValueWithNumKey($array, 0); $val = asoccArrayValueWithNumKey($array, 1); etc... Haven't tryed the code, but i'm pretty sure it will work. Good luck!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions