简体   繁体   English

在CodeIgniter中将数组转换为stdClass

[英]converting array to stdClass in CodeIgniter

I am returning data from two tables in CodeIgniter with the function below 我正在使用以下功能从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();
}

Using var_dump I am getting the result below 使用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)

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. 我似乎无法获得将数组转换为object(stdClass)的方法,对于CodeIgniter的新增功能,将不胜感激。

Use this code in Your Model ( change your table name and select, distinct fileds ) 在您的模型中使用此代码(更改表名并选择不同的文件)

        $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;

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: 通常,您将在foreach循环中使用它,如下所示:

$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()print_r()

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

If you want an array of all properties and their values use get_object_vars() . 如果要使用所有属性及其值的数组,请使用get_object_vars()

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

According to the same documentation for Codeigniter I detail: 根据有关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:`

and .. 和..

result()

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

You should return your data in this way 您应该以这种方式返回数据

return $query->result();

Array to stdClass can be done in php this way. 数组到stdClass可以用这种方式在php中完成。

stdClass:: __set_state(array());

Or a nicer way. 或者更好的方法。

$a = (object) array();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM