Anonymous
Anonymous

Reputation: 1094

getting error Cannot use object of type stdClass as array in php

I am getting values from mysql query as an object so I checked with print_r($result); and got like this

stdClass Object
(
    [user_name] => user1,user2,user3,user4
)

if I try to do like this

echo $result['user_name']; 

then getting error

"Cannot use object of type stdClass as array"

what should be the correct way to echo user1,user2,user3 values

Upvotes: 0

Views: 40

Answers (1)

Danish Ali
Danish Ali

Reputation: 2352

When you get an array of objects

stdClass Object
(
    [user_name] => user1,user2,user3,user4
)

then try to show data like this

echo $result->user_name;

When associative array without objects

Array ( [user_name] => user1, user2, user3, user4 )

Do like this

echo $result['user_name'];

Upvotes: 1

Related Questions