1

Here's my code:

$get_access_keys_from_wp = get_user_meta( $user_id, '_tdv_volunteer_positions' );
$new_access_keys = array();    

if ( $get_access_keys_from_wp ) {
            echo "This user is signed up for other positions.";

            $current_access_keys = $get_access_keys_from_wp;

            foreach($current_access_keys as $key => $value) {
                $new_access_keys[] = $value;
            }
            //$new_access_keys[] = $position_post_id;
            $new_access_keys[] = array('position_id' => $position_post_id, 'volunteer_first_name' => $first_name, 'volunteer_last_name' => $last_name);

        } else {
            echo "This user is not signed up for any positions.";

            //$new_access_keys[] = $position_post_id;
            $new_access_keys[] = array('position_id' => $position_post_id, 'volunteer_first_name' => $first_name, 'volunteer_last_name' => $last_name);

        }

Initially, I get the following:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [position_id] => 67
                    [volunteer_first_name] => Mike
                    [volunteer_last_name] => Jackson
                )

        )

)

And that's great ... I want it like this. But as you can see by the logic above, I need to be able to loop over the array if it already exists, and add to it so that it looks like the following:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [position_id] => 67
                    [volunteer_first_name] => Mike
                    [volunteer_last_name] => Jackson
                )
            [1] => Array
                (
                    [position_id] => 68
                    [volunteer_first_name] => Mike
                    [volunteer_last_name] => Jackson
                )

        )

)

But instead, I'm getting results like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [position_id] => 67
                            [volunteer_first_name] => Mike
                            [volunteer_last_name] => Jackson
                        )

                )

            [1] => Array
                (
                    [position_id] => 68
                    [volunteer_first_name] => Mike
                    [volunteer_last_name] => Jackson
                )

        )

)
1
  • I don't think that copying $get_access_keys_from_wp in $current_access_keys is necessary, since you don't seem to modify either afterwards. Commented Jun 17, 2014 at 22:49

1 Answer 1

2

It seems you need to go 1 level deeper when adding existing values. Try replacing

foreach($current_access_keys as $key => $value) {
    $new_access_keys[] = $value;
}

with

foreach($current_access_keys as $key => $value) {
    $new_access_keys[] = $value[0];
}

or if the existing array contains more than 1 element

foreach($current_access_keys as $key => $value) {
    $new_access_keys = array_merge($new_access_keys,$value);
}
Sign up to request clarification or add additional context in comments.

Comments

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.