0

How can i get the value of a multi array. So i need out of my array the [user_login] and i the [ID] So something like echo $array[user_login] where [ID] is $variable[ID]

I need a solution which works without using echo $array[0][user_login]

Array
(
    [0] => stdClass Object
        (
            [ID] => 15
            [user_login] => Loginname
            [user_nicename] => Loginname
            [user_email] => [email protected]
            [user_url] => http://www.domain.com
            [user_registered] => 2014-10-26 09:39:01
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Test Inc
            [logo] => 
        )

    [1] => stdClass Object
        (
            [ID] => 28
            [user_login] => Loginname
            [user_nicename] => Loginname
            [user_email] => [email protected]
            [user_url] => http://www.domain.com
            [user_registered] => 2014-10-26 09:39:01
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Test Inc
            [logo] => 
        )

    [2] => stdClass Object
        (
            [ID] => 13
            [user_login] => Loginname
            [user_nicename] => Loginname
            [user_email] => [email protected]
            [user_url] => http://www.domain.com
            [user_registered] => 2014-10-26 09:39:01
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Test Inc
            [logo] => 
        )

    [3] => stdClass Object
        (
            [ID] => 11
            [user_login] => Loginname
            [user_nicename] => Loginname
            [user_email] => [email protected]
            [user_url] => http://www.domain.com
            [user_registered] => 2014-10-26 09:39:01
            [user_activation_key] => 
            [user_status] => 0
            [display_name] => Test Inc
            [logo] => 
        )
)
1
  • So you have a variable, like $id, and you need the user_login that corresponds that id? Commented Nov 2, 2014 at 21:19

3 Answers 3

2

You could loop through that array to create a new one:

$newArray = array();
foreach($array as $item){
    $newArray[$item->ID] = $item;
}

Then with that you can access things using $newArray[$id]->whateveritem;

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

4 Comments

Have you tried your code? That's an array of objects, not a multidimensional array
It Wasn't Me. I will test. @Tivie have you a better solution?
@Matthias I was talking to JammyDodger231
Ahh I see now, I have edited it to take the object from the array and split the ID thanks @Tivie
2

That's an array of objects, which is a bit different than an array of arrays (or multidimensional array).

You can loop through the array until you find the correct ID and then grab the user_login. SOmething like this

$myID = 15; //Id to search for

foreach ($array as $obj) {
    if($obj->ID === $myID) {
        echo $obj->user_login; //Or do something with it
    }
}

Comments

0

Items in an array of objects are referenced like $object->member. An easy way to search through this array would be a simple foreach() loop. For example, to find the user with id 28, you would do:

foreach($array as $item) {
    if ($item->ID == '28') {
        echo 'Login is: ' . $item->user_login;
        break;
    }
}

Edit: Oops, Trivie beat me to it

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.