It got me nuts all the time. Basically, I need to set a certain value in a multidimensional array after disinfecting the value and then again (maybe it depends on the check, if the check fails, then the value should be set to an empty string) after checking the value. Let's say I have this message array:
$data['should']['be']['int'] = ' yjd';
After clearing the value with filter_var( $value, FILTER_SANITIZE_NUMBER_INT ); I get an empty string. Then I needed to somehow set the value to $data['should']['be']['int'] as an empty string.
This value is then passed to the validation function, which fails because the empty string is not an integer. Again, this confirmed value will need to be set to $data['should']['be']['int'] an empty string.
Before the whole check starts, I save all the relevant keys in an array, so by the time I need to sanitize or check, I have something like this:
$keys = array( 0 => 'should', 1 => 'be', 2 => 'int' );
I tried to access the $data array using the above keys in the foreach loop, specifying the &$data array to set a new value, but could not, no matter what I tried. The above is just a simplified example. This is all part of the validation class, so I donโt know the exact depth of the past $data array.
Any pointers would be greatly appreciated! Thank you for your help!
Edit: Thought that I could not edit the message, but it turned out to be just an Internet connection. Please ignore my comment below. In any case, here is the method that I tried to call recursively:
protected function set_value( &$data, $value ) { foreach( $data as &$val ) { if( is_array( $val ) ) { $this->set_value( $val, $value ); } else { $val = $value; } } }
To start the loop, I did the following:
$this->set_value( $data[$keys[0]], $value );