patrickzdb
patrickzdb

Reputation: 1073

Perform foreach on values returned inside array

I'm not sure if I have worded this question correctly, but basically I have the following array result from WordPress I am trying to deal with:

Array
(
    [0] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 3
                    [user_login] => Jean Gomes
                    [user_pass] => $P$BvW6LK9/blGKLuSj.9spNPW3.v104..
                    [user_nicename] => jean-gomes
                    [user_email] => [email protected]
                    [user_url] => 
                    [user_registered] => 2014-08-28 10:56:39
                    [user_activation_key] => 
                    [user_status] => 0
                    [display_name] => Jean Gomes
                )

            [ID] => 3
            [caps] => Array
                (
                    [author] => 1
                )

            [cap_key] => dpa2335327_capabilities
            [roles] => Array
                (
                    [0] => author
                )

            [allcaps] => Array
                (
                    [upload_files] => 1
                    [edit_posts] => 1
                    [edit_published_posts] => 1
                    [publish_posts] => 1
                    [read] => 1
                    [level_2] => 1
                    [level_1] => 1
                    [level_0] => 1
                    [delete_posts] => 1
                    [delete_published_posts] => 1
                    [wpseo_bulk_edit] => 1
                    [author] => 1
                )

            [filter] => 
        )

    [1] => WP_User Object
        (
            [data] => stdClass Object
                (
                    [ID] => 4
                    [user_login] => Tammy Day
                    [user_pass] => $P$BejydpFxKDp60WruV71u0/JnJm9o.E.
                    [user_nicename] => tammy-day
                    [user_email] => [email protected]
                    [user_url] => 
                    [user_registered] => 2014-08-28 10:57:23
                    [user_activation_key] => 
                    [user_status] => 0
                    [display_name] => Tammy Day
                )

            [ID] => 4
            [caps] => Array
                (
                    [author] => 1
                )

etc

I would like to perform a foreach using these results, for the ID.

E.g.

foreach ( $authors as $author_id ) {

But I don't know how to access the ID. I tried:

foreach ( $authors->ID as $author_id ) {

But obviously this can't work, as there is a number before that, is there an equivalent of:

foreach ( $authors['X']->ID as $author_id ) {

that I could use?

Sorry I know this is probably a silly question, but I'm quite stuck. I know I probably have to do a foreach within a foreach but I can't work out the syntax at all.

Upvotes: 0

Views: 71

Answers (3)

Nicolai
Nicolai

Reputation: 5797

foreach (array_expression as $value)

On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).

Read this documentation page for more details about foreach loop:

In your situation $value will pointer to the instance of WP_User Object. To access to the user's id just read object's property ID

foreach ( $authors as $author ) {
    $author_id = $author->ID;
}

Upvotes: 1

hchr
hchr

Reputation: 317

As the dump states, the array elements are instances of WP_User. You can use the following loop to fetch the IDs:

foreach ($authors as $User) {
    $author_id = $User->ID;
    // Act
}

For a reference of the WP_User class, see http://codex.wordpress.org/Class_Reference/WP_User

Upvotes: 1

Kunal Gupta
Kunal Gupta

Reputation: 449

You cant try this instead of foreach :

$i=0;
while ($i<count($authors)){
$author_id = $authors[$i]['data']['id'];
//Do whatever you want here.
$i=$i+1;
}

Upvotes: 1

Related Questions