1

Say, if I have a PHP array:

$arr = array(
    "a"=>"value a",
    "b"=>"value b",
    "0"=>"value 0",
    "1"=>"value 1",
);

and if I want to retrieve the element at index 0, how would I do that?

5
  • Are you looking for $arr["0"]? Commented Feb 4, 2015 at 6:35
  • 3
    For index 0 specifically there is reset(). For any other case: convert it to an indexed array first. Commented Feb 4, 2015 at 6:35
  • <?php echo $arr[0]; ?> Commented Feb 4, 2015 at 6:36
  • index 0 means element at first position with variable index a? Commented Feb 4, 2015 at 6:37
  • Call reset, and call each. Commented Feb 4, 2015 at 6:38

4 Answers 4

5

Other answers clearly didn't read it. This solution will work for you:

echo $arr[array_keys($arr)[0]];

And here's an Example


References

You could even use array_values() to get the first element:

echo array_values($arr)[0];

You could even alternatively do this:

$keys = array_keys($arr);
echo $arr[$keys[0]];

Basically, array_keys() fetches the keys (which in this example would be 'a','b','0','1') and stores them in the array. Allowing you to access the very first item via $keys[0] to access the first key (Being a).


Alternative Solution

This solution is purely based off @mario's comment as it is a completely viable method of achieving what you require!

Alternatively, you could use reset() and current() to acheive this, where reset() sets the index to the very first element in your array, and current() shows the current element in that array, which would be the first, due to the reset.

reset($arr);
echo current($arr);

Example


References

These are the ways in which you'll be able to achieve what you desire. As you won't know what the very first array elements' index is.

Sign up to request clarification or add additional context in comments.

3 Comments

"Other answers clearly didn't read it" - No, the question is ambiguous, due to the presence of "0" => "value 0". "At index 0" could refer to element "value a" (the first element in order) or to element "value 0" (element whose key is 0), depending how you interpret the question. There is no need (or, indeed, any basis) for belittling others.
@Amadan He states "at index 0" (meaning the very first element), not "for index 0" as you've just perceived. I agree with you about it being ambiguous, but if you read it and take it in, you'll see exactly what he was asking for.
There is nothing "clearly" about it, and I did read it (and take it in). Not all askers are native English speakers, nor do they use the most appropriate terms (and speaking of which, PHP's terminology is whack anyway, since "arrays" are not actually arrays, for one thing). So no, I don't see exactly what he was asking for. "meaning the very first element" is just a guess (validated post-facto by the green checkmark), since in absence of OP's clarification (or some degree of telepathic ability) it was impossible to know for sure what that sentence meant - thus my question-comment under the OP.
1

There is no "index 0" in your case because PHP arrays internally are not arrays in the original sense, but kind of an ordered hashmap. But you can use array_values to get a 0-indexed array:

$element = array_values($array)[0];

This is a generic solution for all indexes. For the first and the last element, it's faster to use reset and end if you can live with the side effect of changing the internal array pointer:

$first = reset($array);
$last = end($array);

1 Comment

Thanks. That's what's been said in the answer above: stackoverflow.com/a/28314980/843732
0

To set an array to its values you should use "array_values", but you would lose the keys.

I would suggest to save your keys first

$keys = array_keys($arr);

Then use

array_values($arr);

Your result is really a flat array where you can use the index to get the values

$arr = array(
    "value a",
    "value b",
    "value 0",
    "value 1",
);

After your done you can combine the keys back with the array

$restored = array_combine($keys, $arr);

Comments

-1

The above solutions will help you but the one more alternative solution is=>

foreach($array as $key=>$value)
{
 if($value==0)
  {
  $new_array[]=$key;
  }
}

new array will have key with 0 index.

1 Comment

This isn't even close to what was asked... You are checking the value as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.