arianhojat Posted July 3, 2008 Share Posted July 3, 2008 I am making a recursive function to modify an array, and its not modifying the arrays under the main array. So like when the function approaches a subarray, it recursively calls the function passed by reference.... i see then it changes that subarray via 'CHANGED array...' called right after it hits the required field in that subarray. But when that subarray gets done, and it then prints 'CHANGED array...' for the array again, it doesnt change the sub value. the initial parent #required value is the only one changed permanently for some reason; Not sure why cause even recursively it should respect the '&' pass-by-reference sign in the function parameters right? code is attached if u want to run and let me know what the hecks i doing wrong... <?php function unset_required_fields(&$array){ foreach( $array as $key=>$value) { echo '#KEY being looked at: ['.$key.']<br/>'; echo '$array be4:<pre>'.htmlspecialchars(print_r($array, true)).'</pre><br/>'; if( is_array($value) ) { echo 'RECURSE, look at array under ['. $key .']...'.'<br/>'; unset_required_fields($value); } else if( $key == 'required') { echo 'CHANGED array, value for this "required" key should now be 0!!!'.'<br/>'; $array[$key] = 0; //'#required' } else echo 'Nothing cool happens since not an array or required field'.'<br/>'; echo '$array AFTER:<pre>'.htmlspecialchars(print_r($array, true)).'</pre><br/>'; } } $thearray = array( 'title' => "Here are some radio button questions", 'required' => 1, 'weight' => 2, 'child 1' => array( 'title' => "Radio button 1 question: blah blah?", 'required' => 1, 'weight' => -1 ), 'someotherparameters0' => 0, 'child 2' => array( 'title' => "Radio button 2 question: blah blah?", 'required' => 1, 'weight' => -1 ), 'someotherparameters1' => 0, 'someotherparameters2' => "bleh" ); unset_required_fields($thearray); ?> [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/113117-recursive-function-to-modify-array-not-working/ Share on other sites More sharing options...
papaface Posted July 3, 2008 Share Posted July 3, 2008 I think it should be: <?php function unset_required_fields(&$array){ foreach( $array as $key=>$value) { echo '#KEY being looked at: ['.$key.']<br/>'; echo '$array be4:<pre>'.htmlspecialchars(print_r($array, true)).'</pre><br/>'; if( is_array($value) ) { echo 'RECURSE, look at array under ['. $key .']...'.'<br/>'; unset_required_fields($value); } else if( $key == 'required') { echo 'CHANGED array, value for this "required" key should now be 0!!!'.'<br/>'; $array[$key] = 0; //'#required' } else { echo 'Nothing cool happens since not an array or required field'.'<br/>'; } echo '$array AFTER:<pre>'.htmlspecialchars(print_r($array, true)).'</pre><br/>'; } } $thearray = array( 'title' => "Here are some radio button questions", 'required' => 1, 'weight' => 2, 'child 1' => array( 'title' => "Radio button 1 question: blah blah?", 'required' => 1, 'weight' => -1 ), 'someotherparameters0' => 0, 'child 2' => array( 'title' => "Radio button 2 question: blah blah?", 'required' => 1, 'weight' => -1 ), 'someotherparameters1' => 0, 'someotherparameters2' => "bleh" ); unset_required_fields($thearray); ?> Quote Link to comment https://forums.phpfreaks.com/topic/113117-recursive-function-to-modify-array-not-working/#findComment-581074 Share on other sites More sharing options...
arianhojat Posted July 3, 2008 Author Share Posted July 3, 2008 Didnt work. You just added brackets around the 'else' statement, right? Which arent needed for 1 line else statement anyway. Thanks for trying but i think its something bigger that im not seeing/understanding how pass by reference works in php. You can see at the end of the code the subarrays required fields arent modified, even though when it handles the subarrays it looks like it does change them. Anyone got ideas? Quote Link to comment https://forums.phpfreaks.com/topic/113117-recursive-function-to-modify-array-not-working/#findComment-581104 Share on other sites More sharing options...
wildteen88 Posted July 3, 2008 Share Posted July 3, 2008 Use: function unset_required_fields(&$array) { foreach($array as $key => $value) { if( is_array($value) ) { unset_required_fields($array[$key]); } else if( $key == 'required') { $array[$key] = 0; } } } Any changes made to the $value variable will not have any affect on the passed array. When running unset_required_fields function within your loop you should pass the actual array itself. Quote Link to comment https://forums.phpfreaks.com/topic/113117-recursive-function-to-modify-array-not-working/#findComment-581125 Share on other sites More sharing options...
arianhojat Posted July 3, 2008 Author Share Posted July 3, 2008 Wow that makes alot of sense, thanks wildteen88, you are some sort of Golden God! I praise your holy statue from now on. Quote Link to comment https://forums.phpfreaks.com/topic/113117-recursive-function-to-modify-array-not-working/#findComment-581129 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.