我怎样才能在php中访问stdclass的值

时间:2015-04-13 06:37:31

标签: php arrays stdclass

我想访问下图中的int20和int30。我怎么能这样做?

这是我的var_dump结果:

array (size=2)
  0 => 
    array (size=1)
      0 => 
        object(stdClass)[238]
          public 'time' => int 20
  1 => 
    array (size=1)
      0 => 
        object(stdClass)[242]
          public 'time' => int 30

这是我的疑问:

$sum_time = array();
foreach ($a as $f) {
    $sum_time[] = DB::select("select time from quiz where '$f'=id");
}        
var_dump($sum_time);

3 个答案:

答案 0 :(得分:1)

将此添加到您的代码中以获取所有“时间”值的数组

$times = array();
foreach ($sum_time as $value) {
    $times[] = $value[0]->time;
}

答案 1 :(得分:0)

    $sum_time = array();
    $time = array();
    $count=0;
    foreach ($a as $f) {
        $sum_time[] = DB::select("select time from quiz where '$f'=id");
        $time[]=$sum_time[$count]->time;
        $count++;
    }   
print_r($time)     
    var_dump($sum_time);

答案 2 :(得分:-1)

您可以将->object用于stdClass。 实施例

$v = new StdClass();
$v->name = "Mohsen";
$v->addr = "somewhere";

$tmp = array("data" => "other data","detail" => $v);
var_dump($tmp);

echo "<br /><br />";
echo $tmp["detail"]->name; // Access your stdClass object