59

I have an array like:

$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
echo native_function($array, 0); // bar
echo native_function($array, 1); // bin
echo native_function($array, 2); // ipsum

So, this native function would return a value based on a numeric index (second arg), ignoring assoc keys, looking for the real position in array.

Are there any native function to do that in PHP or should I write it? Thanks

3
  • 3
    This sounds like a bad idea. What do you need this for? Would it not be much preferable to actually have numeric keys in this case, and the foo value as a member of a sub-array?
    – Pekka
    Commented Jun 18, 2011 at 13:22
  • I also think, that this looks like bad design. Can you tell us what you are trying to do? Maybe there is better solution.
    – Alp
    Commented Jun 18, 2011 at 13:25
  • PHP is bad design! Looks like there are different types of arrays in PHP. You have to convert one array to another array to actually access the items via index.
    – Bitterblue
    Commented Jul 24, 2018 at 7:05

4 Answers 4

114
$array = array('foo' => 'bar', 33 => 'bin', 'lorem' => 'ipsum');
$array = array_values($array);
echo $array[0]; //bar
echo $array[1]; //bin
echo $array[2]; //ipsum
1
  • 1
    At least in non-ancient versions of php, array_values($array)[0] works fine for a reference, if you're not going to need to use it over and over. Iteratively yours would be the better option.
    – Blue Dev
    Commented Apr 19, 2022 at 16:10
21

array_values() will do pretty much what you want:

$numeric_indexed_array = array_values($your_array);
// $numeric_indexed_array = array('bar', 'bin', 'ipsum');
print($numeric_indexed_array[0]); // bar
1
  • ...and, as of PHP 5.4, crush it down to: print (array_values($your_array)[0]);
    – Pancho
    Commented Jul 30, 2015 at 18:27
8

I am proposing my idea about it against any disadvantages array_values( ) function, because I think that is not a direct get function. In this way it have to create a copy of the values numerically indexed array and then access. If PHP does not hide a method that automatically translates an integer in the position of the desired element, maybe a slightly better solution might consist of a function that runs the array with a counter until it leads to the desired position, then return the element reached.

So the work would be optimized for very large array of sizes, since the algorithm would be best performing indices for small, stopping immediately. In the solution highlighted of array_values( ), however, it has to do with a cycle flowing through the whole array, even if, for e.g., I have to access $ array [1].

function array_get_by_index($index, $array) {

    $i=0;
    foreach ($array as $value) {
        if($i==$index) {
            return $value;
        }
        $i++;
    }
    // may be $index exceedes size of $array. In this case NULL is returned.
    return NULL;
}
2
  • But what if false would be a fetched value? Returning null would be still bad but better than false.
    – user619271
    Commented Jun 8, 2016 at 11:36
  • Considering the consistency of the type, I would get the value NULL or if the input index exceeds the size of the array, or if the target value from the index input is NULL. In this case I agree with you. However FALSE and NULL give the same result on the function empty(), which would be the next check would go to carry out in any scenario, it makes sense to call this function.
    – and.ryx
    Commented Jun 8, 2016 at 12:45
6

Yes, for scalar values, a combination of implode and array_slice will do:

$bar = implode(array_slice($array, 0, 1));
$bin = implode(array_slice($array, 1, 1));
$ipsum = implode(array_slice($array, 2, 1));

Or mix it up with array_values and list (thanks @nikic) so that it works with all types of values:

list($bar) = array_values(array_slice($array, 0, 1));
2
  • 1
    +1 Though I would have used list($bar) = array_slice($array, 0, 1) to remove the scalar value limitation ;)
    – NikiC
    Commented Jun 18, 2011 at 13:43
  • @nikic: I thought about that, unfortunately, array_slice will preserve string keys (even if preserve_keys is false), so you can't use list in that case, unless you array_values the resulting array. Anyway, I guess that is also a solution, edited. Thanks.
    – netcoder
    Commented Jun 18, 2011 at 13:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.