Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2005, 10:10 AM
Registered User
 
Join Date: Jun 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default appending to nested array

I'm working on the "xml parse to keyed-array" post from yesterday. The problem is much more fundamental that parsing the XML. I simply do not understant how php's "referencing" works.

As an example, I try the following:

// start out with an array containing just "0"
$root = array(0);
// make $root the "parent" for the next array to go inside
$parent =& $root;
// add 5 more arrays, one inside the other
for ($i=1; $i<=5; $i++) {
    // an array containing just $i
    $newArray = array($i);
    // push this onto the end of $parent
    $parent[] = $newArray;
    // make the new array the new parent
    $parent =& $newArray;
    // show use what $root looks like now
    print_r($root);
    echo "<br/>";
}

what I am expecting is a growing nest like this:


Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 [1] => Array([0] => 2) ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 [1] => Array([0] => 2 [1] => Array([0] => 3)) ) )
...etc.

BUT, instead I just get this:

Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )

So, something is wrong with my referencing, right?

Thanks for any help.

Andrew

 
Old March 2nd, 2005, 10:38 AM
Registered User
 
Join Date: Jun 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BTW, I tried changing line 10 below from a simple assignement to a reference, and I got recursion. Why is this?

Thanks!

// start out with an array containing just "0"
$root = array(0);
// make this the "parent" for the next array to go inside
$parent =& $root;
// add 5 more arrays
for ($i=1; $i<=5; $i++) {
    // an array containing just $i
    $newArray = array($i);
    // push this onto the end of $parent
    $parent[] =& $newArray;
    // make the new array the new parent
    $parent =& $newArray;
    // show use what $root looks like now
    print_r($root);
    echo "<br/>";
}


Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 2 [1] => Array *RECURSION* ) )
Array ( [0] => 0 [1] => Array ( [0] => 3 [1] => Array *RECURSION* ) )
Array ( [0] => 0 [1] => Array ( [0] => 4 [1] => Array *RECURSION* ) )
Array ( [0] => 0 [1] => Array ( [0] => 5 [1] => Array *RECURSION* ) )






Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending Files indupriyav Other Programming Languages 0 March 12th, 2008 05:57 AM
Appending values austinf XSLT 2 May 9th, 2006 11:21 PM
chapter 4 - appending a substring al_b_cnu BOOK: Beginning Java 2 6 July 10th, 2004 08:52 AM
Appending to a TextFile jfleming SQL Server DTS 3 June 16th, 2004 08:45 AM
APPENDING TO A RECORD sinner Classic ASP Databases 4 June 2nd, 2004 01:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.