PHP函数OOP数组拉

时间:2014-01-05 18:13:58

标签: php

目前我的文件结构如下所示,一切正常。 但在我的home.php中,我希望能够拉出阵列的一部分,例如用户图像。

$connect->LoadInformation->image 

我将如何编写此代码,或更改我的代码以使其工作?我真的希望这是有道理的。

home.php

     $connect->LoadInformation($_COOKIE['steamID']);

class.php

        public function LoadInformation($steamID){

        $query = "SELECT * FROM user WHERE steamid = '$steamID'";
        $results = $this->con->get_results( $query );


        echo "<pre>";
        print_r($results);
        echo "<pre>";
    }

当前输出

    Array
(
    [0] => Array
        (
            [id] => 10
            [steamid] => 76561198026169223
            [profilestate] => 0
            [steam_user] => Dz - Lol u mad?
            [online] => 0
            [signup] => 2014-01-05 17:28:15
            [image] => http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b85a568b25210a27a8413e4dc05c9f42a7cdaf22.jpg
            [description] => 
            [active] => 0
            [admin] => 0
        )

)

2 个答案:

答案 0 :(得分:2)

只需使用(object) ...

将其转换为对象即可
public function LoadInformation($steamID){
   $query = "SELECT * FROM user WHERE steamid = '$steamID'";
   $results = $this->con->get_results( $query );
   return (object) array_shift($results);
}

使用它:$connect->LoadInformation(...)->image;

我猜测 steamid 在表格中是唯一的,在这种情况下,只需从数据库查询中选择第一个返回的结果,然后根据需要使用它。

答案 1 :(得分:1)

像这样更改 class.php ..

public function LoadInformation($steamID){
        $query = "SELECT * FROM user WHERE steamid = '$steamID'";
        $results = $this->con->get_results( $query );
        return $results;
    }

home.php

$arr =  $connect->LoadInformation($_COOKIE['steamID']);
echo $arr[0]['image']; //<-- prints  http://media.steampowered.com/steamcommunity/public/images/avatars/b8/b85a568b25210a27a8413e4dc05c9f42a7cdaf22.jpg