-1

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 ?

4
  • 1
    Using unset($key) no ? what is the problem ? Commented Sep 11, 2015 at 7:53
  • I have try that but didn't work out. Also, unset key of what ? From $recursive ? Also the key referes to the current array in recursion. Commented Sep 11, 2015 at 7:54
  • Define a var : $get = $_GET; then remove using unset($get[$key]); and use $get after. Commented Sep 11, 2015 at 7:58
  • @VincentDecaux thanks for your reply, but this doesn't work too. I just try it. Commented Sep 11, 2015 at 8:00

2 Answers 2

-1

You can use in loop to remove key item unset($key)

Sign up to request clarification or add additional context in comments.

1 Comment

It did not work, because you unset variable $key : ) If you want unset array element then do unset($recursive[$key] )
-2

People who recommend to unset $key, you must read manual. Unsetting $key just unset variable $key - not array element! If you want to unset array element you must apply unset to them. In this case:

unset($recursive[$key])

3 Comments

As you read in the documentation this is a NOT TO USE. This producing the following : Fatal error: Cannot use object of type RecursiveIteratorIterator as array in ..../core.php on line 89.
Oh, i missed that is not array
@MerianosNikos I read a little about this problem and seems were no way to delete element in recursive iterator. But you can do this without iterator, check this topic stackoverflow.com/questions/9150726/… Hope it help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.