dongsi0625 2013-11-09 23:46
浏览 67
已采纳

在CodeIgniter中将数组转换为stdClass

I am returning data from two tables in CodeIgniter with the function below

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();
}

Using var_dump I am getting the result below

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)

But what I will like to get is this

  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)

I can't seem to get a way of converting the array into object(stdClass) Any help will be appreciated as am new to CodeIgniter.

  • 写回答

4条回答 默认 最新

  • dtoq41429 2013-11-09 23:49
    关注

    use

    return $query->result();
    

    This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this:

    $query = $this->db->query("YOUR QUERY");
    
    foreach ($query->result() as $row)
    {
       echo $row->title;
       echo $row->name;
       echo $row->body;
    }
    

    Edit:

    If you just want to print you can use var_dump() or print_r().

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

    If you want an array of all properties and their values use get_object_vars().

    $properties = get_object_vars($obj);
    print_r($properties);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?