php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64431 SPL RecursiveArrayIterator::offsetSet with ArrayObject fails but stdClass works
Submitted: 2013-03-15 15:29 UTC Modified: 2014-01-06 09:08 UTC
From: namarpi at yahoo dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.4.13 OS: Windows
Private report: No CVE-ID: None
 [2013-03-15 15:29 UTC] namarpi at yahoo dot com
Description:
------------
Trying to modify an array through RecursiveIteratorIterator iterator. The successful modification depends on how the array object was constructed.

Fails: $array_object = new ArrayObject( $array, 0, "RecursiveArrayIterator" );

Works: $object = json_decode( json_encode( $array ));

I am confused, because I expect the same behavior on both iterators. Attached a detailed test script, which tells more than my bad English text. Maybe I am escaping something?


Test script:
---------------
http://madarak.net/php_reports/spl-recursion.txt

Expected result:
----------------
Modified ArrayObject object after using RecursiveArrayIterator::offsetSet/offsetUnset methods.

Actual result:
--------------
There are no effects on ArrayObject object after calling RecursiveArrayIterator::offsetSet/offsetUnset methods.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-15 18:30 UTC] namarpi at yahoo dot com
Relocated the file to a GhitHub repository:

https://github.com/namarpi/php-reports/blob/master/test-RecursiveArrayIterator::offsetSet.txt
 [2013-03-16 07:36 UTC] namarpi at yahoo dot com
This test script gets rid of the RecursiveIteratorIterator, and focuses only on the RecursiveArrayIterator.

https://github.com/namarpi/php-reports/blob/master/test-RecursiveArrayIterator::getChildren::offsetSet.php
 [2013-03-17 06:25 UTC] namarpi at yahoo dot com
Perhaps you have already faced with that too, that method chaining fails even with stdClass. If not than this script provides an example:

https://github.com/namarpi/php-reports/blob/master/insertGrandchildByGrandparentKey.php
 [2013-03-18 05:47 UTC] namarpi at yahoo dot com
Hello again,

I would like to offer my help with some simple test scripts, which will be collected at

https://github.com/namarpi/PHP-SPL-Recursive-Examples

If you need them use them.
 [2013-03-20 19:17 UTC] namarpi at yahoo dot com
Hello,

good news: method chaining works (leastwise two levels), so apology for the rumor. Here is the script which contains two solutions, one for RecursiveIteratorIterator, and one for RecursiveArrayIterator. It would be really nice to complete this wiki page: http://en.wikipedia.org/wiki/Method_chaining :))

https://github.com/namarpi/php-reports/blob/master/methodChain.php
 [2013-03-22 07:54 UTC] namarpi at yahoo dot com
I think it might be interesting:

Manual says: public void ArrayIterator::offsetSet ( string $index , string $newval )
but thank to Developer it works with offsetSet ( string $index , array/stdClass $newnode ) too.

https://github.com/namarpi/php-reports/blob/master/offsetSet-string-and-array.php
 [2013-03-27 17:01 UTC] namarpi at yahoo dot com
I would like to insert new nodes into the array with iterator, but the solutions in the previously linked test script were not consistent. So, found a better way to insert and modify new nodes dynamically, and append them to the iterator on the fly. This works so smooth and nice that it must be a good solution in the case of stdClass.

https://github.com/NaMarPi/php-reports/blob/master/insertNewNode.php
 [2014-01-06 09:08 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2014-01-06 09:08 UTC] ab@php.net
Hi,

grateful thanks for your offer with the test scripts. I think it had much more sense to turn your scripts into teh phpt test cases and then do a PR on github. Please see http://qa.php.net/howtohelp.php for how to write test cases, and please  reduce the test code to the absolute required minimum. The smaller the repro code is, the easier it is to debug errors.

Cheers
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 00:01:29 2024 UTC