|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-06-13 18:58 UTC] acallaha at connect dot carleton dot ca
Description:
------------
ArrayObject::offsetGet() does NOT return the value corresponding to a given index as documented, but actually requires a key (NOT an int $index) as input and returns a value given this key. This was determined through testing.
Reproduce code:
---------------
A sample array:
$array_3=array("this"=>"0", "is"=>"1", "life"=> "hello");
$array_3_obj = new ArrayObject($array_3);
print $array_3_obj->offsetGet(2);
Expected result:
----------------
hello
Actual result:
--------------
PHP Notice: Undefined offset: 2
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 20 17:00:01 2025 UTC |
This is expected behavior for ArrayObject:offsetGet(). The array in your reproduce code has only associative keys, so there is no value at the integer index 2: $ php <?php $array_3=array("this"=>"0", "is"=>"1", "life"=> "hello"); $array_3_obj = new ArrayObject($array_3); print $array_3_obj->offsetGet(2)."\n"; print $array_3_obj->offsetGet("is")."\n"; $array_4_obj = new ArrayObject(array("0","1","hello")); print $array_4_obj->offsetGet(2)."\n"; ?> ^D Notice: Undefined offset: 2 in - on line 4 1 hello $ If you truly want the third element of an associative array regardless of key, you may be looking for ArrayIterator.