Jump to content

recursive function to modify array not working


arianhojat

Recommended Posts

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]

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.