Shoaib Shoaib - 5 months ago 31
PHP Question

Get the single element from the array

I got follows array after print the whole array from db how i can get a single element like e_domains from this printed array:

stdClass Object
(
[id] => 1
[uni_origin] => Aachen
[e_domains] => rwth-aachen.de
)


I have print this array like this:

if ($results ) {
foreach ( $results as $result ){
echo'<pre>'; print_r($result) ;
//echo $result->('e_domains');
}
}

Answer Source

First off, that's not an array, that's an object. Like it says: "stdClass Object".

Access object properties like this:

$object->property_name

In your case, it would be:

$result->e_domains

There are much more to learn on the subject, like static properties, visibility etc. In your case, the above example will work.

Read more about classes and objects in the manual: http://php.net/manual/en/language.oop5.basic.php