Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array element indexed by numerical string

Tags:

php

I have encountered something odd.

I have a php array, indexed with numerical keys. However it appears impossible to access any of the elements because php automatically treats numerical strings as integers, causing an illegal offset notice.

Under normal circumstances its imposable to create a php array with numerical string indexes, but it can happen with type casting.

To reproduce:

$object = new stdClass();
$object->{'1'} = 'one';

$array = (array) $object;

var_dump($array);
/* produces
array(1) {
  ["1"]=>
  string(3) "one"
}
*/

//none of the following will work
$key = '1';
echo $array[1], $array['1'], $array["1"], $array[(string)1], $array[$key];

Is this just an edge case bug? I only encountered the problem when attempting to improve my answer for another SO question

Live code example: http://codepad.viper-7.com/dFSlH1

like image 435
Steve Avatar asked Nov 13 '15 15:11

Steve


People also ask

How do you access an array of strings?

We can access an array value through indexing, placed index of the element within square brackets with the array name. Declaration and initialization of string array in a single line: String array can also be declared and initialized in a single line. This method is more recommended as it reduces the line of code.

What is difference between numeric and string index array?

The answer is simple: there is no difference. Javascript arrays are objects. All keys of objects are strings (or symbols), but never numbers. So array[0] is interpreted as array['0'] .

How to access the elements of an array in C#?

C# array accessing elements. After an array is created, its elements can be accessed by their index. The index is a number placed inside square brackets which follow the array name. We can use the index from end ^ operator to get elements from the end of the array.

Can you access a string like an array c++?

Strings can be declared, written and printed directly in C++. Also, each character in a string can be accessed using an index similar to indexing in the array.


2 Answers

Unbelievable but this is normal behavior in php, it was considered as a bug (link) in the year 2008.

But they just pointed out to the manual for the cast with (array):

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible;

You can use get_object_vars() instead:

$object = new stdClass();
$object->{'1'} = 'one';

$array = get_object_vars( $object );

$key = '1';
echo $array[1]."<br>";
echo $array['1']."<br>";
echo $array["1"]."<br>";
echo $array[(string)1]."<br>";
echo $array[$key]."<br>";

Doesn't explain why this happens, but is a solution to avoid the cast problem.

Off topic but I thought maybe it is interesting. Found this in the manual.

To avoid these kind of problems, always use an integer OR a string as index, don't mix it up and don't use integers in a string.

Example of mixed array:

$array = array(
    1    => "a",
    "1"  => "b",//overrides 1
    1.5  => "c",//overrides "1"
    true => "d",//overrides 1.5
);

var_dump($array);
like image 129
swidmann Avatar answered Oct 10 '22 12:10

swidmann


You can use

$vars  = get_object_vars($object);
echo $vars[1];
like image 36
wogsland Avatar answered Oct 10 '22 11:10

wogsland