-2

This is my array:

$wp_session = array(
    'id' => $id,
    'id2' => $id2,
    'p1' => $p1,
    'p2' => $p2
);

The result was giving me a Recursive_ArrayAccess so I searched on how to fix it and came across these answers:

PHP - Recursive Multidimension Array iterator PHP foreach() with arrays within arrays?

So I added/changed my foreach statement to:

    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($wp_session); 

    //Split the parameters with &
    foreach ($iterator as $key => $value) {
            if(!empty($value)){
                $fields .= $key . '=' . $value . '&';
            }
            //Remove the last &
            $fields2 = substr($fields, 0, -1);
    }

But then the page breaks.

I also tried:

    $iterator = array_walk_recursive($wp_session);

The page doesn't break but $fields2 is not outputting the data in the next foreach statement. This is the whole code:

    //Start a session

    $wp_session = WP_Session::get_instance();

    //Get the current parameters

    $id = $_GET['id'];
    $id2 = $_GET['id2'];
    $p1 = $_GET['p1'];
    $p2 = $_GET['p2'];
    $wp_session = array(
        'id' => $id,
        'id2' => $id2,
        'p1' => $p1,
        'p2' => $p2
    );

    //array_walk_recursive($wp_session);
    //$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($wp_session); 
    $iterator = array_walk_recursive($wp_session);

    //Split the parameters with &
    foreach ($iterator as $key => $value) {
            if(!empty($value)){
                $fields .= $key . '=' . $value . '&';
            }
            //Remove the last &
            $fields2 = substr($fields, 0, -1);
    }

    if( is_admin() )
        return $items;

    //Get the menu items and add the above parameters to the menu items urls

    foreach( $items as $item ) 
    {
        if($wp_session){
            $item->url .= '?' . $fields2;
        }
    }
    return $items;

Can someone please explain what I am doing wrong? Any kind of help or suggestions will be appreciated!

1
  • 1
    Its breaking because you missed the closing ) for RecursiveIteratorIterator Commented Jul 21, 2014 at 8:23

2 Answers 2

2

But then the page breaks.

That page has to break:

 $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($wp_session); 

Count the number of ( and then the number of ) and then start using a good IDE. If you still didn't get the hint, you are 1 ) short on that line.

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

2 Comments

Thanks, didn't see it. Will find a better editor. Can I ask, why isn't array_walk_recursive($wp_session); passing the data to the next foreach statement?
Because you are not using that function as it is supposed to. Seems like there is any misunderstanding. This is what that function does: array_walk_recursive — Apply a user function recursively to every member of an array But you are sending it only 1 parameter :)
2

Overkill, it seems you simply want to create a parameter string from the array.

There's http_build_query() for that:

$fields = http_build_query($wp_session);

See it in action

1 Comment

I didn't really go through the whole code they have but if that is really what they want to do then that's really a big overkill. Almost funny too :) +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.