Reputation: 69
Which one would you use?
Basically I only want to get the 1st element from a array, that's it.
Upvotes: 5
Views: 3515
Reputation: 3776
$arr[0]
only works if the array as numerical keys.
array_shift
removes the element from the array and it modifies the array itself.
If you are not sure what the first key is , and you do not want to remove it from the array, you could use:
<?php
foreach($arr $k=>$v){
$value = $v;
break;
}
or even better:
<?php
reset($arr);
$value = current($arr);
Upvotes: 4
Reputation: 82048
You would use $arr[ 0 ];
array_shift
removes the first element from the array.
This answer is actually somewhere between incomplete and plain out wrong but, because the comments of the two jon
's I think that it should actually stay up so that others can see that discourse.
The right answer:
reset
is the method to return the first defined index of the array. Even in non-associative arrays, this may not be the 0
index.array_shift
will remove and return the value which is found at reset
The OP made the assumption that $arr[0]
is the first index is not accurate in that particular context.
Upvotes: 6
Reputation:
If you don't want to change the array in question, use $arr[0]
(which merely gets the first element), otherwise if you want to remove the first element of $arr
from $arr
, use array_shift($arr)
.
For example:
$arr=array(3,-6,2);
$foo=$arr[0]; //$foo==3 and $arr==array(3,-6,2).
$bar=array_shift($arr); //$bar==3 and $arr==array(-6,2).
ETA: As others have pointed out, be sure that your array isn't an associative array (ie the keys are 0,1,...,(sizeof($arr)-1)
), otherwise this probably won't work.
Upvotes: 1
Reputation: 1181
If you want the first element of an array, use $arr[0] form. Advantages - Simplicity, Readability and Maintainability. Keep things straight forward.
Edit: Use index 0 only if you know that the array has default keys starting from 0.
Upvotes: 1
Reputation: 33904
If you have an associative Array you can also use reset($arr)
: It returns the first Element (doesn't remove), and sets the array pointer to this element.
But the fastest way is $arr[0]
.
Upvotes: 2
Reputation: 1246
with array_shif you have two operations:
if you access by index, actually you have only one operation.
Upvotes: 1
Reputation: 360762
arrshift is more reliable and will always return the first element in the array, but this also modifies the array by removing that element.
arr[0] will fail if your array doesn't start at the 0 index, but leaves the array itself alone.
A more convoluted but reliable method is:
$keys = array_keys($arr);
$first = $arr[$keys[0]];
Upvotes: 1
Reputation: 8425
given what you need, $arr[0] is preferrable, because it's faster. array_shift is used in other situations.
Upvotes: 1
Reputation: 78743
Do you want to modify the arr
array also? array_shift
removes the first element of the array and returns it, thus the array has changed. $arr[0]
merely gives you the first element.
I would use $arr[0]
unless I explicitly wanted to modify the array. You may add code later to use the arr
array and forget that it was modified.
Upvotes: 1
Reputation: 385274
Well, they do different things.
array_shift($arr)
takes the first element out of the array, and gives it to you.
$arr[0]
just gives it to you... if the array has numeric keys.
An alternative that works for associative arrays too is reset($arr)
. This does move the array's internal pointer, but unless you're using those functions this is unlikely to affect you.
Upvotes: 16
Reputation: 13198
array_shift
will actually remove the specified value from the array. Do not use it unless you really want to reduce the array!
See here: http://php.net/manual/en/function.array-shift.php
Upvotes: 13