OZONE Asylum
Forums
Server-Side Scripting - Oh my!
PHP seems to "forget" changes made to an array in a loop.
This page's ID:
30951
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
I'm trying to make a code for a game site that will convert strings resembling the following... [code]*Legend of Zelda: Ocarina of Time **Index **Glitches ***Swordless Link ****Poe Link ***Steal the Fishing Rod **Videos **Guides **Secrets **Cheats[/code] Into an array structure resembling the following: [code]array( 'Legend of Zelda: Ocarina of Time'=> array( 'Index'=>array(), 'Glitches'=> array( 'Swordless Link'=> array( 'Poe Link'=>array() ), 'Steal the Fishing Rod'=>array() ), 'Videos'=>array(), 'Guides'=>array(), 'Secrets'=>array(), 'Cheats'=>array() ) );[/code] Now, to do this, I explode the string at each newline ([i]$treeData = explode("\n", $myString)[/i]) and use a foreach to loop through the array of lines. I process each line, using them to build another array, [i]$tree[/i]. Basically, once that foreach loop ends, [i]$tree[/i] reverts back to the value it had before any of the looping happened. My code: [code]<pre><?php function OutputTreeView($treeString,$root=0) { $treeData=explode("\n",$treeString); // array of lines $tree = array(); $curs = array(); $offset = 0; // not important if($root) { // FROM HERE TO THE NEXT COMMENT... IS NOT IMPORTANT. $line=$treeData[0]; $offset=strspn($line,'*'); $level=0; $tree[substr($line,$level+$offset)] = array(); $curs[$level]=substr($line,$level+$offset); array_shift($treeData); } else { $level=0; $line=''; $tree['[root]']=array(); $curs[$level]='[root]'; } // FROM HERE TO THE PREVIOUS COMMENT IS NOT IMPORTANT. foreach($treeData as $line) { $level= strspn($line,'*')-$offset; // use the "*"s at the beginning to determine how nested we are. $curs[$level]=substr($line,$level+$offset); // used to keep track of where we are in $tree, so that we can build $varString $varString = '$tree['; // prepare for variable substitution (the double dollar sign trick) for($i=0;$i<=$level;$i++) { $varString.="'".$curs[$i]."']"; if($i<$level) { $varString.='['; } } $$varString = array(); // the actual variable substitution, used to alter $tree // [debug statement removed] if($level+1<count($curs)) { // this IF and the WHILE inside of it is only NEEDED for debugging. while(count($curs)>$level+1) { array_pop($curs); } } } return $tree; } $TREE='*Legend of Zelda: Ocarina of Time'."\n".'**Index'."\n". '**Glitches'."\n".'***Swordless Link'."\n".'****Poe Link'."\n". '***Steal the Fishing Rod'."\n".'**Videos'."\n".'**Guides'."\n". '**Secrets'."\n".'**Cheats'; // The string example I presented above. I woulda used a nowdoc, // but PHP was throwing a T_SL error even though there was nothing // wrong with the syntax (I Googled the error to figure out the cause, // and the cause wasn't present) -- ah, well. echo OutputTreeView($TREE); ?></pre>[/code] Using a large amount of debug statements (most of which have been removed to shorten the code), I am able to tell that $tree is modified during the loop, but these modifications don't "stick" at the end of the loop. Now, $tree isn't the array I'm looping through, so AFAIK I don't need to place an ampersand before it ("&$tree" shouldn't be necessary)... Plus a debug statement after the loop, checking $$varString, shows the modifications that [i]should[/i] be visible just by checking $tree normally... And some debug output that'll probably help to clarify what my code actually [i]does[/i]: [code]Function invoked! $treeString == *Legend of Zelda: Ocarina of Time **Index **Glitches ***Swordless Link ****Poe Link ***Steal the Fishing Rod **Videos **Guides **Secrets **Cheats $root == 1 so "*Legend of Zelda: Ocarina of Time" is the "root" of this list, and $offset = 1. Iteration! $level==1, $line==**Index, $varString==$tree['Legend of Zelda: Ocarina of Time']['Index'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==1, $line==**Glitches, $varString==$tree['Legend of Zelda: Ocarina of Time']['Glitches'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==2, $line==***Swordless Link, $varString==$tree['Legend of Zelda: Ocarina of Time']['Glitches']['Swordless Link'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==3, $line==****Poe Link, $varString==$tree['Legend of Zelda: Ocarina of Time']['Glitches']['Swordless Link']['Poe Link'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==2, $line==***Steal the Fishing Rod, $varString==$tree['Legend of Zelda: Ocarina of Time']['Glitches']['Steal the Fishing Rod'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==1, $line==**Videos, $varString==$tree['Legend of Zelda: Ocarina of Time']['Videos'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==1, $line==**Guides, $varString==$tree['Legend of Zelda: Ocarina of Time']['Guides'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==1, $line==**Secrets, $varString==$tree['Legend of Zelda: Ocarina of Time']['Secrets'] [here, we set $$varString = array()] $$varString==Array Iteration! $level==1, $line==**Cheats, $varString==$tree['Legend of Zelda: Ocarina of Time']['Cheats'] [here, we set $$varString = array()] $$varString==Array Function complete. And when we print_r $tree, we see: ( [Legend of Zelda: Ocarina of Time] => Array ( ) ) ...Which is weird, because $tree['Legend of Zelda: Ocarina of Time'] shouldn't be empty. And when we print_r $$varString, we see: Array ( ) Which makes sense. And when we print_r $curs, we see: Array ( [0] => Legend of Zelda: Ocarina of Time [1] => Cheats )[/code] I am thoroughly confused. ---------------------- [url=http://www.ozoneasylum.com/30926][img]http://img19.imageshack.us/img19/268/cellu.gif[/img][/url][url=http://davidjcobb.deviantart.com/][img]http://img262.imageshack.us/img262/428/24464470.gif[/img][/url][url=http://getfirefox.com/][img]http://img515.imageshack.us/img515/6336/25338231.gif[/img][/url][img]http://img4.imageshack.us/img4/2163/end.gif[/img] [small](Edited by [url=http://www.ozoneasylum.com/user/6997]DavidJCobb[/url] on 04-09-2009 23:28)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »
Maximum Security
OZONE
DHTML/Javascript
Server-Side Scripting - Oh my!
CSS - DOM - XHTML - XML - XSL - XSLT
Stupid Basic HTML
Visual Therapy
Photoshop
Photoshop Pong, Anyone?
***WARNING*** BIG SIG APPROACHING
Photography
3D Modelling & Rendering
Multimedia/Animation
Print Graphics
Holding Pens
Philosophy and other Silliness
Outpatient Counseling
Site reviews!
Mad Scientists' Laboratory
Getting to know the Grail