19

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?

pgiecek
  • 7,970
  • 4
  • 39
  • 47
myol
  • 8,857
  • 19
  • 82
  • 143
  • 3
    By default, PHP also use int as keys, starting from 0. – Clément Malet Jul 18 '14 at 12:41
  • 2
    Arrays *may* be real arrays, or *may* be Hashmaps; it's one type for both, and yes, this can be very confusing. There's no built-in way to check for this (see: http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-sequential?rq=1). Note that some functions/operations can convert an "Array" to a "Hashmap" or vice versa ... The `array` type is one of the major design flaws of the language IMHO... – Martin Tournoij Jul 18 '14 at 12:41
  • @Clément Andraud - when I var dump or console log I simply get 'Array'. If I count($array) I get something like '35'. So I know there are values, but I cannot figure out how to access them – myol Jul 18 '14 at 12:47
  • 2
    You cannot console.log a PHP array - you have to use var_dump($array) or print_r($array) in your PHP code replacing the $array variable name with the actual variable name that holds the array. – Jay Blanchard Jul 18 '14 at 12:49
  • Jay Blanchard - I did not know that, thanks. Is there a nice way to format the output? I just get a wall of code.. – myol Jul 18 '14 at 12:52
  • 1

3 Answers3

41

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;
}
deceze
  • 510,633
  • 85
  • 743
  • 889
1

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.

Bobby
  • 292
  • 1
  • 12
1

If you want to access every N-th element:

$n = 3;
foreach (array_keys($arr) as $i => $key) {
    if (($i+1) % $n) {
        continue;
    }

    $value = $arr[$key];
}
phoenix
  • 1,629
  • 20
  • 11