简体   繁体   中英

Get the single element from the array

I got the following array (the array is retrieved through a db query). Now, my question is, how do I get a single element like e_domains from the array mentioned below:

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

I got the output shown above by running the following line of codes:

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

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

Try this:

$e_domains = mysql_result(mysql_query("SELECT id FROM games LIMIT 1"),0);

Hope it helpt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM