php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36219 By Reference nested arrays
Submitted: 2006-01-31 02:50 UTC Modified: 2006-01-31 04:16 UTC
From: bdhook at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.2 OS: Slackware 10.2
Private report: No CVE-ID: None
 [2006-01-31 02:50 UTC] bdhook at gmail dot com
Description:
------------
I have been writing some code to handle Modified Pre-ordered Tree Traversal data, and the sole purpose of the code is to avoid recursive calls. The code works, but I found I had to place a line in that should not need to be there.

My arrays are nested, but by reference rather than as usual. Each item with children has a member array called 'children', with each value bein a reference to a child. Each child has a member called 'parent', which has a reference to the parent array. The problem is, my code (which functions by using internal array pointers) doesn't work properly until the array is copied by reference back into itself. The code posted on my website (which will remain up as long as I don't have an answer) demonstrates this, though it needs a populated database with an MPTT tree to see it in action.

The line $array=&$array['children'][0]['parent']; should not need to be there. But, if you remove it, the code doesn't function correctly until after its natural cycle brings it back up to the root node, which effectively mimics the by reference assignment. At this point in the code the counter is already thrown off.

Note that $array=&$array; did not seem to work to fix the problem.

Reproduce code:
---------------
http://www.coder7.com/mptt.phps

Expected result:
----------------
The code generates the expected output, but only because a work-around has been put in.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-31 04:16 UTC] bdhook at gmail dot com
Turns out this was a bug in my code. The code went through several revisions, and at one point the argument to the function was changed so that it was not by reference. This caused the function to get a *copy* of the root node at start, and get the proper reference after cycling through the code a few times. Adding a & in the correct place caused the code to function as expected without the "fix" line.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 15:01:29 2024 UTC