dk4210's avatar

(PHP) - Find key value in an associative array

Hello Guys,

I tying to figure out how to get the value of the key "user" in this case it's "bob"

This is my array

Array
(
    [timeStamp] => 2020-01-20 16:25:23.000443
    [referenceCode] => 
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 554343543535435
                    [clientId] => 333222
                    [clientOid] => tysossoss
                    [dbUrl] => xxxxxxxx
                    [user] => bob
                    [pwd] => xxxxxx
                    [schema] => extended
                    [deleted] => 
                    [new] => 
                    [createdBy] => admin
                    [createdOn] => 
                    [modifiedBy] => null
                    [modifiedOn] => 
                )

        )

    [stepup] => 
    [messages] => Array
        (
        )

)

Please advise

0 likes
6 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60
$array['data'][0]['name']; // this should give you the name
dk4210's avatar

Awesome! That worked.

what if I wanted to get say 3 values based on their respective keys? How would I do that?

would I have to do that in a foreach loop?

Sergiu17's avatar

Like so

foreach($array['data'] as $user)
{
    if($user['name'] === 'bob') {
        print_r($user);
    }
}
dk4210's avatar

Thanks! I meant I want to get the value of the following keys (For example)

clientOid
schema
createdBy

Sergiu17's avatar

@dk4210

foreach($array['data'] as $user)
{
    if($user['name'] === 'bob') {
        echo $user['clientOid'], ' ', $user['schema'], ' ', $user['createdBy'];
    }
}
dk4210's avatar

Perfect! Thank you so much!

1 like

Please or to participate in this conversation.