0

我使用以下函数从 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

4 回答 4

1

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

stdClass:: __set_state(array());

或者更好的方式。

$a = (object) array();
于 2014-12-12T14:21:43.320 回答
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;
于 2018-07-27T07:31:43.023 回答
0

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

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();
于 2013-11-10T00:23:58.220 回答
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);
于 2013-11-09T23:49:48.200 回答