1

I have this:

print_r($response["member"]);

I need to retrieve name under levels, it's at the bottom, how should I write it: I thought of $response["member"][0]["Sequential"]["levels"]...? Also this number under levels wont be always the same.

Thank you!

Array
(
    [0] => Array
        (
            [ID] => 1
            [UserInfo] => Array
                (
                    [ID] => 1
                    [caps] => Array
                        (
                            [administrator] => 1
                        )

                    [cap_key] => wp_capabilities
                    [roles] => Array
                        (
                            [0] => administrator
                        )

                    [allcaps] => Array
                        (
                            [switch_themes] => 1
                            [edit_themes] => 1
                            [activate_plugins] => 1
                            [edit_plugins] => 1
                            [edit_users] => 1
                            [edit_files] => 1
                            [manage_options] => 1
                            [moderate_comments] => 1
                            [manage_categories] => 1
                            [manage_links] => 1
                            [upload_files] => 1
                            [import] => 1
                            [unfiltered_html] => 1
                            [edit_posts] => 1
                            [edit_others_posts] => 1
                            [edit_published_posts] => 1
                            [publish_posts] => 1
                            [edit_pages] => 1
                            [read] => 1
                            [level_10] => 1
                            [level_9] => 1
                            [level_8] => 1
                            [level_7] => 1
                            [level_6] => 1
                            [level_5] => 1
                            [level_4] => 1
                            [level_3] => 1
                            [level_2] => 1
                            [level_1] => 1
                            [level_0] => 1
                            [edit_others_pages] => 1
                            [edit_published_pages] => 1
                            [publish_pages] => 1
                            [delete_pages] => 1
                            [delete_others_pages] => 1
                            [delete_published_pages] => 1
                            [delete_posts] => 1
                            [delete_others_posts] => 1
                            [delete_published_posts] => 1
                            [delete_private_posts] => 1
                            [edit_private_posts] => 1
                            [read_private_posts] => 1
                            [delete_private_pages] => 1
                            [edit_private_pages] => 1
                            [read_private_pages] => 1
                            [delete_users] => 1
                            [create_users] => 1
                            [unfiltered_upload] => 1
                            [edit_dashboard] => 1
                            [update_plugins] => 1
                            [delete_plugins] => 1
                            [install_plugins] => 1
                            [update_themes] => 1
                            [install_themes] => 1
                            [update_core] => 1
                            [list_users] => 1
                            [remove_users] => 1
                            [add_users] => 1
                            [promote_users] => 1
                            [edit_theme_options] => 1
                            [delete_themes] => 1
                            [export] => 1
                            [administrator] => 1
                        )

                    [filter] => 
                    [user_login] => admin
                    [user_nicename] => admin
                    [user_email] => [email protected]
                    [user_url] => 
                    [user_registered] => 2014-01-29 10:57:09
                    [user_activation_key] => 
                    [user_status] => 0
                    [display_name] => admin
                    [wlm_feed_url] => http://pialarson.com/excel/feed/?wpmfeedkey=1;2e7e48ca65d94e5f0ec1baae46e4972c
                    [wpm_login_date] => 1392155735
                    [wpm_login_ip] => 62.68.119.252
                )

            [Sequential] => 
            [Levels] => Array
                (
                    [1391447566] => stdClass Object
                        (
                            [Level_ID] => 1391447566
                            [Name] => Team Membership
                            [Cancelled] => 
                            [CancelDate] => 
                            [Pending] => 
                            [UnConfirmed] => 
                            [Expired] => 
                            [ExpiryDate] => 1393866766
                            [SequentialCancelled] => 
                            [Active] => 1
                            [Status] => Array
                                (
                                    [0] => Active
                                )

                            [Timestamp] => 1391447566
                            [TxnID] => WL-1-1391447566
                        )

                )

            [PayPerPosts] => Array
                (
                )

        )

)
1
  • Sequential doesn't have anything in it, try doing just Levels. Commented Feb 11, 2014 at 22:25

4 Answers 4

1

An answer might be to use array_walk_recursive by following the official documentation: http://www.php.net/manual/en/function.array-walk-recursive.php

<?php

$properties = new stdClass();
$properties->names = [];
function extractNames($levels, $key, $properties) {
    if (
        is_object($levels) &&
        array_key_exists('Name', get_object_vars($levels)) &&
        array_key_exists('Level_ID', get_object_vars($levels))
    ) {
        $properties->names[] = $levels->Name;
    }
}
array_walk_recursive($response, 'extractNames', $properties);
echo print_r($properties, true);
Sign up to request clarification or add additional context in comments.

9 Comments

I see, so if i have multiple names, it will add them to array. This is what i was looking for,thanks!
There might be a reference missing somewhere (or perhaps replace $names array by another standard object such as $names = new StdClass(); would help even more)... I'm trying to prove it working by building a reference array but you have the main idea!
hmm i am getting syntax error, unexpected T_FUNCTION
Which version of PHP are you using?
Again big thanks Thierry, ill go with first one for now
|
1
<?php
   $nameInFirstLevelsElement = current($response["member"][0]["levels"])->Name
?>

This should work to retrieve the Name from the first levels element.

Comments

1

That empty space next to "Sequential" means it doesn't have a value. So that's not the one you're looking for.

Furthermore, one of those levels indicates "stdClass object", which means you can access its members via the -> operator.

Let's strip away everything that doesn't matter for a minute. I think it'll help you understand the data structure:

Array
(
    [0] => Array
        (
            [Levels] => Array
                (
                    [1391447566] => stdClass Object
                        (
                            [Level_ID] => 1391447566
                            [Name] => Team Membership
                        )
                )
        )

)

So this will work:

$object = $response["member"][0]["Levels"][1391447566];
$name = $object->Name;

Edit

If the index of Levels changes every time, then pull it apart a little bit more...

$levels = $response["member"][0]["Levels"];
$firstLevel = array_shift(array_values($levels));
$name = $firstLevel->Name;

See here for a good answer on getting the first element out of the $levels array: https://stackoverflow.com/a/3771228/266374

4 Comments

Aha, this number "1391447566" will be diffrent every time, is it possible to somehow skip it or how would we do then ?
array_shift(array_values($levels)) -> why not simply current($levels)?
@Flixer In case an associative array is returned. I don't know anything about this API, so I'd rather be safe than sorry.
hey @ThomasKelley if i want to retrieve not one but all levels, how would it look like ?
1

If the number under the 'Levels' array won't be the same, you can use a foreach to pull out the 'Name' information.

foreach ($response[0]['Levels'] AS $level_key => $level_val) {
    $level_name = $level_key->Name;
}

echo 'Name: '.$level_name;

If there is only going to be one element, then it will grab it. If there are multiple numbers under 'Levels', then it will loop through them and assign each one to '$level_name', overwriting any previous assignments. In other words, only the last one it finds will be captured.

EDIT: In the example, I mistakenly tried to grab the Name from the $key instead of the $val. This is the correct method:

foreach ($response[0]['Levels'] AS $level_key => $level_val) {
    $level_name = $level_val->Name;
}

echo 'Name: '.$level_name;

Here is a demo of the working code

1 Comment

Well, you're correct. I have a typo in my code. You should be getting the Name from the val, not the key. I will make an update in my post with the correct syntax.

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.