在CodeIgniter中将数组转换为stdClass

时间:2013-11-09 23:46:00

标签: php codeigniter

我使用下面的函数

从CodeIgniter中的两个表返回数据
public function test()
{
    $this->db->select('*');
    $this->db->from('WHOUSE1.DLY_BWR_DLY_PERFORMANCE');
    $this->db->join('WHOUSE1.DATE_DIM', 'WHOUSE1.DATE_DIM.DATE_KEY = WHOUSE1.DLY_BWR_DLY_PERFORMANCE.BDP_DATE');
    $query = $this->db->get();
    return $query->result_array();
}

使用var_dump我得到的结果是

array (size=3226)
0 => 
array (size=121)
  'BDP_ID' => string '945149' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040807' (length=8)
  'BDP_DAY_CODE' => string '6' (length=1)
  'BDP_TAKE' => string '4923.78' (length=7)
  'BDP_PAYOUT' => string '3779.22' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)

1 => 
array (size=121)
  'BDP_ID' => string '945150' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040809' (length=8)
  'BDP_DAY_CODE' => string '1' (length=1)
  'BDP_TAKE' => string '2848.3' (length=6)
  'BDP_PAYOUT' => string '4190.34' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)

但我希望得到的是

  array (size=3226)
  0 => 
  object(stdClass)[27]
  'BDP_ID' => string '945149' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040807' (length=8)
  'BDP_DAY_CODE' => string '6' (length=1)
  'BDP_TAKE' => string '4923.78' (length=7)
  'BDP_PAYOUT' => string '3779.22' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '636' (length=3)

  1 => 
  object(stdClass)[29]
  'BDP_ID' => string '945150' (length=6)
  'BDP_COST_CENTRE_NUMBER' => string '1376' (length=4)
  'BDP_DATE' => string '20040809' (length=8)
  'BDP_DAY_CODE' => string '1' (length=1)
  'BDP_TAKE' => string '2848.3' (length=6)
  'BDP_PAYOUT' => string '4190.34' (length=7)
  'BDP_ACTUAL_SLIPPAGE' => string '280' (length=3)

我似乎无法获得将数组转换为对象的方法(stdClass)任何帮助都将成为CodeIgniter的新手。

4 个答案:

答案 0 :(得分:1)

在您的模型中使用此代码(更改表名并选择不同的文件)

        $this->db->select('DISTINCT(subcategory)');
        $this->db->from('tbl_property');  
        $this->db->where('status','1');                          
        $data           = $this->db->get()->result(); 
        $sub_id         = array();
        foreach ($data as $row)
        {
            array_push($sub_id,$row->subcategory);
        }
        $this->db->from('tbl_subcategory');  
        $this->db->where_in('id',$sub_id);                          
        $data1          = $this->db->get()->result();
        return $data1;

答案 1 :(得分:0)

使用

return $query->result();

此函数将查询结果作为对象数组返回,或者在失败时返回空数组。通常你会在foreach循环中使用它,如下所示:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

修改

如果您只想打印,可以使用var_dump()print_r()

var_dump($obj);
print_r($obj);

如果您想要所有属性的数组及其值使用get_object_vars()

$properties = get_object_vars($obj);
print_r($properties);

答案 2 :(得分:0)

根据Codeigniter I的相同文档详细说明:

result_array()

This function returns the query result as a pure array, or an empty array when no 
result `is produced. Typically you'll use this in a foreach loop, like this:`

和..

result()

This function returns the query result as an array of objects, or an empty array 
on failure.

您应该以这种方式返回数据

return $query->result();

答案 3 :(得分:0)

stdClass的数组可以用这种方式在php中完成。

stdClass:: __set_state(array());

或者更好的方式。

$a = (object) array();