php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34816 Inconsitent behavior in ArrayObject for multi-dimensional data
Submitted: 2005-10-10 19:42 UTC Modified: 2005-10-10 20:30 UTC
From: adove at booyahnetworks dot com Assigned: helly (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.1.0RC1 OS: WinXP
Private report: No CVE-ID: None
 [2005-10-10 19:42 UTC] adove at booyahnetworks dot com
Description:
------------
If you create an ArrayObject with mutli-dimensional array data, you can access unique element paths fine but you can not change/add any multi-dimensional paths due to the dreaded "Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference" error. Note this happens in BOTH 5.0.5 AND 5.1.0RC1.

IMHO, either the documentation for ArrayObject should clearly indicate that it supports uni-dimenisional data get/set and ONLY get for multi-dimensional. OR, the object walk the passed data and turn all arrays into ArrayObject instances? 

Reproduce code:
---------------
$a = array(
    "test" => array(
        "one" => "dunno",
        "two" => array(
            "peekabo" => "do you see me?",
            "anyone" => array("there")
            )
        )
    );
$oArray = new ArrayObject($a);
var_dump($oArray);
echo "\n\\test\\one == " . $oArray["test"]["one"] . "\n\n";

// NEITHER of the two below will work!
$oArray["test"]["one"] = "Yes I do!"; 
$oArray["test"]["yes"] = array(
    "hello" => "Goodbye!"
    );
var_dump($oArray);

Expected result:
----------------
object(ArrayObject)#1 (1) {
  ["test"]=>
  array(2) {
    ["one"]=>
    string(5) "dunno"
    ["two"]=>
    array(2) {
      ["peekabo"]=>
      string(14) "do you see me?"
      ["anyone"]=>
      array(1) {
        [0]=>
        string(5) "there"
      }
    }
  }
}

\test\one == dunno

object(ArrayObject)#1 (1) {
  ["test"]=>
  array(2) {
    ["one"]=>
    string(5) "Yes I do!"
    ["two"]=>
    array(2) {
      ["peekabo"]=>
      string(14) "do you see me?"
      ["anyone"]=>
      array(1) {
        [0]=>
        string(5) "there"
      }
    }
    ["yes"]=>
    array(1) {
        ["hello"]=>
        string(8) "Goodbye!"
    }
  }
}


Actual result:
--------------
object(ArrayObject)#1 (1) {
  ["test"]=>
  array(2) {
    ["one"]=>
    string(5) "dunno"
    ["two"]=>
    array(2) {
      ["peekabo"]=>
      string(14) "do you see me?"
      ["anyone"]=>
      array(1) {
        [0]=>
        string(5) "there"
      }
    }
  }
}

\test\one == dunno

Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in array_obje_test.php on line 16


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-10 19:51 UTC] derick@php.net
marcus, this is your baby :)
 [2005-10-10 20:23 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

ArrayObject/ArrayIterator simply implement the ArrayAccess imnterface which doesn\'t handle multi dimensional array syntax. As you pointer out already you could have ArrayObject store ArrayObjects itself. Thus the solution is to overwrite ArrayObject.
 [2005-10-10 20:30 UTC] adove at booyahnetworks dot com
Ok, I wanted to confirm what my suspicion on the answer was going to be and you guys have, so thanks for that. HOWEVER, I really think you may want annotate the ArrayObject documentation as PHP makes array (the data type) access very flexible and the docs for the ArrayObject class indicate it would not be "recursively" compliant. 

In any case, I'll add such a note to the online docs and will be deriving my own addtional class.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC