Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access N-th element of an array in PHP

Tags:

arrays

php

I'm embarrassed to ask this and it's most likely a duplicate, but my google results are coming up short (im searching incorrectly I guess) and such a basic question is infuriating me.

I have an array containing values I don't know.

In java, to have a look at the 2nd entry, I would use something like

var = array[1]

I understand Php arrays are key-value pairs, but how can I simply look at the nth value in an array to see it's key-value pair, and even better, then access just the key / value?

like image 858
myol Avatar asked Jul 18 '14 12:07

myol


People also ask

How do you access individual elements of an array?

We can access elements of an array using the index operator [] . All you need do in order to access a particular element is to call the array you created. Beside the array is the index [] operator, which will have the value of the particular element's index position from a given array.

Which can be used for accessing the nth element of an array named arr?

In this line, println() method will show the message in the screen of the enter users, who want to know the nth position element in the array. Line 4 : int n = sc. nextInt(); Here once the user input some number it will store in the variable with name “n”.

How do you find the second value in an array?

To get the second to last element in an array, call the at() method on the array, passing it -2 as a parameter, e.g. arr.at(-2) . The at method returns the array element at the specified index.

How can we retrieve an element from an array?

Retrieving the first and last elements in a simple array can most easily be done by using the ARRAY_FIRST and ARRAY_LAST functions. Retrieving the next or previous elements in a simple array can most easily be done by using the ARRAY_PRIOR and ARRAY_NEXT functions.


2 Answers

If your keys are numeric, then it works exactly the same:

$arr = ['one', 'two', 'three']; // equivalent to [0 => 'one', 1 => 'two', 2 => 'three']
echo $arr[1]; // two

If your keys are not numeric or not continuously numeric, it gets a bit trickier:

$arr = ['one', 'foo' => 'bar', 42 => 'baz'];

If you know the key you want:

echo $arr['foo']; // bar

However, if you only know the offset, you could try this:

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

Or numerically reindex the array:

$values = array_values($arr);
echo $values[1];

Or slice it:

echo current(array_slice($arr, 1, 1));

Most likely you want to be looping through the array anyway though, that's typically what you do with arrays of unknown content. If the content is unknown, then it seems odd that you're interested in one particular offset anyway.

foreach ($arr as $key => $value) {
    echo "$key: $value", PHP_EOL;
}
like image 64
deceze Avatar answered Sep 29 '22 07:09

deceze


If you want to access the nth element without knowing the index, you can use next() n times to reach the nth element.

for($i = 0; $i<$n; $i++){
    $myVal = next();
}
echo $myVal;

There are other ways to access a specific element, already mentioned by @deceze.

like image 31
Bobby Avatar answered Sep 29 '22 05:09

Bobby