从数组中获取单个元素

Get the single element from the array

我得到了以下数组(该数组是通过数据库查询检索到的)。现在,我的问题是,如何从下面提到的数组中获取像 e_domains 这样的单个元素:

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

我通过 运行 以下代码行得到上面显示的输出:

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

首先,这不是一个数组,而是一个对象。就像它说的:"stdClass Object".

像这样访问对象属性:

$object->property_name

在你的情况下,它将是:

$result->e_domains

关于这个主题还有很多东西需要学习,比如静态属性、可见性等。在您的情况下,上面的示例会起作用。

阅读手册中有关 类 和对象的更多信息:http://php.net/manual/en/language.oop5.basic.php

试试这个:

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

希望对您有所帮助。