I try to remove a key / value from a $_GET string in case a given trigger string matches.
If the get was one level array that's simple to solve it, but because the $_GET can contains nested arrays, I have a litle problem.
Let's say I have the following $_GET string:
?nikos=vasilis&merianos=nikos&greece[corfu]=ionian%20islands&greece[corfu]=west%20greece&another_var[l1][l2][]=trigger-1&another_var[l1][l2][trigger-2]=value
that translated into the following by using print_r:
Array
(
[nikos] => vasilis
[merianos] => nikos
[greece] => Array
(
[corfu] => west greece
)
[another_var] => Array
(
[l1] => Array
(
[l2] => Array
(
[0] => trigger-1
[trigger-2] => value
)
)
)
)
In order to match the trigger string, that in my case is the trigger-1 and trigger-2 i use the following code:
$iterator = new \RecursiveArrayIterator( $_GET );
$recursive = new \RecursiveIteratorIterator( $iterator, \RecursiveIteratorIterator::SELF_FIRST );
$triggers = array(
'trigger-1',
'trigger-2'
);
foreach ( $recursive as $key => $value ) {
if ( in_array( $key, $triggers ) || in_array( $value, $triggers ) ) {
echo "I found it";
}
}
What I need, is when the trigger matches to remove te given key from the current itterated array.
So, what is the best way to perform this action ?
- UPDATE 1 -
I just try this, but strill doesn't work:
$iterator->getInnerIterator()->offsetUnset( $key );
Any idea why ?
unset($key)no ? what is the problem ?$recursive? Also the key referes to the current array in recursion.$get = $_GET;then remove usingunset($get[$key]);and use $get after.