php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41682 offsetGet() returns a VALUE, given a KEY
Submitted: 2007-06-13 18:58 UTC Modified: 2007-06-13 19:33 UTC
From: acallaha at connect dot carleton dot ca Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Fedora
Private report: No CVE-ID: None
 [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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-13 19:33 UTC] gwynne@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 30 22:00:03 2025 UTC