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] => goranefbl@gmail.com
                    [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
                (
                )

        )

)
Fleshgrinder
  • 15,703
  • 4
  • 47
  • 56
Goran Jakovljevic
  • 2,714
  • 1
  • 31
  • 27

4 Answers4

1
<?php
   $nameInFirstLevelsElement = current($response["member"][0]["levels"])->Name
?>

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

Flixer
  • 938
  • 1
  • 7
  • 20
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

Community
  • 1
  • 1
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
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);
Th. Ma.
  • 9,432
  • 5
  • 31
  • 46
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

Quixrick
  • 3,190
  • 1
  • 14
  • 17
  • 1
    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. – Quixrick Mar 12 '14 at 13:17