Accessing an associative array by integer index in PHP

Use array_keys.

$keys = array_keys($my_arr);
$my_arr[$keys[1]] = "not so much bling"; 

There is no correlation between numeric and associative index keys.

When you say you want to set the value of an associative array using the array index of the key/value, then you have to use the given key, setting $array[1] is not the same as setting $array['foo'].

Consider this array

print_r( array('foo', 'foo' => 'bar', 'baz', 'some' => 'value') );

This will give

Array
(
    [0] => foo
    [foo] => bar
    [1] => baz
    [some] => value
)

The foo is the second element in the array. That's the offset, but it has nothing to do with the index 1. As you can see, in that array above, index 1 is associated with baz. It is wrong to assume that just because foo is the first associative key it has anything to do with the actual numeric key 1. Just like some does not correlate to 2.

Likewise, for a mixed array like shown above, the solution with array_keys suggested elsewhere on this site will not work, because

print_r( array_keys(array('foo', 'foo' => 'bar', 'baz', 'some' => 'value')) );

will give

Array
(
    [0] => 0
    [1] => foo
    [2] => 1
    [3] => some
)

So when you do $array[$keys[1]] you are really doing $array['foo']. But if you wanted to access the second associative value in that array ('some'), you cannot do $array[$keys[2]] because that would evaluate to $array[1] and that's baz.

The Offset of an element is completely unrelated to it's key or value

print_r(
    array(
        100    => 'foo',
        'foo'  => 'bar',
        50     => 'baz',
        'some' => 'value'
    )
);

really means

Array
( //key       value     offset/position
    [100]  => foo       // 0
    [foo]  => bar       // 1
    [50]   => baz       // 2
    [some] => value     // 3
)

which means the element at offset 0 is foo although it's key is 100. If you want to extract elements from an array by offset, you have to use

$third = array_splice($array, 2, 1);
echo $third[0]; // baz

This would create an array holding only the element at the third position.

Or you could use an ArrayIterator. The ArrayIterator implements a Seekable interface that lets you seek to a specific position/offset in the array and then fetch that:

$iterator = new ArrayIterator($array);
$iterator->seek(3);
echo $iterator->current(); // value

Whilst array_keys() allows access to the nth key, array_values will give you the nth value.

<?php
$array = [
   0     => 'Zero',
   '1'   => 'One',
   'Two' => 'Two',
];
echo array_values($array)[2];
?>

will output 'Two'.

Is there an advantage of one over the other? Well, the only minor one I can see is the number of array accesses.

With array_keys() you need to 3.

  1. Get the keys from the data array.
  2. Get the nth key from the list of keys.
  3. Get the value using the nth key from the data array.

With array_values(), you only need 2.

  1. Get the values from the data array.
  2. Get the nth value from the list of values.

But, on the other hand, keys are normally smaller and the data could be hugely nested, so, on balance, using the array_keys() is probably safer.