Dave Doni
Dave Doni

Reputation: 25

Trying to print or echo an array in an array

I have an array called $results, when I use function:

print_r($results);

I get the following.

Array
(
    [0] => ProfileElement Object
        (
            [name] => John thomson 
            [email] => [email protected]
            [Bio] => 20 years of engineering expertise
            [url] => http://twitter.com 
        )
)

My goal is to echo [name] [email] [Bio] [url] values separately. But when I write the following code in php I don't get any values?

echo $results[0]["ProfileElement Objects"]["Bio"];

Does anyone know why? Isn't this an array inside an array?

Upvotes: 2

Views: 123

Answers (6)

jpatiaga
jpatiaga

Reputation: 352

It is an object. You can get the 'Bio' value using:

echo $results[0]->Bio;

Upvotes: 1

Crashspeeder
Crashspeeder

Reputation: 4311

It's an object inside an array. It looks like you should be able to access it as $results[0]->name, $results[0]->email, etc.

Upvotes: 1

Jakub
Jakub

Reputation: 20475

Try doing:

$results[0]->name;

ProfileElement Object is the object type.

Upvotes: 1

trembon
trembon

Reputation: 768

remove ["ProfileElement Objects"]

echo $results[0]->Bio;

Upvotes: 2

Madara's Ghost
Madara's Ghost

Reputation: 174957

You were close.

echo $results[0]->bio;

Is probably what you want. $result[0] is an object.

Also, depending on visibility, you may need to use a getter method.

Upvotes: 5

webbiedave
webbiedave

Reputation: 48897

It appears that the array element contains an object, not another array. To access the object property, use the -> operator:

echo $results[0]->Bio;

Upvotes: 9

Related Questions