我在下面的查询中使用了ID为键值的数组结果,
$this->db->select('gp.id, gp.lot_no, SUM(gb.weight) AS weight, SUM(gb.staple) AS staple, SUM(gb.mic) AS mic, SUM(gb.strength) AS strength, SUM(gb.trash) AS trash, gb.color_grade');
$this->db->from(GIN_BALES . ' gb');
$this->db->join(GIN_PROCESS . ' gp', 'gp.id=gb.process_id');
$this->db->where('gb.sold_status', 0);
$this->db->where('gp.ginner_id', $this->prscr_id);
$this->db->where('gp.program', $program_id);
$this->db->group_by('gp.id');
$lot_details = $this->db->get()->result();
但是我得到如下数组结果,并且数组键的值为0.1。我想要ID为561,562的数组键。
Array
(
[0] => stdClass Object
(
[id] => 561
[lot_no] => 1
[weight] => 16230
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
[1] => stdClass Object
(
[id] => 562
[lot_no] => 2
[weight] => 15523
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
)
任何人都可以为该查询问题提供解决方案吗?
答案 0 :(得分:1)
这是一支班轮,
// null will take id as key and whole array as value
$temp = array_column($temp, null, "id");
参考:array_column。
Demo。
输出:
Array
(
[561] => stdClass Object
(
[id] => 561
[lot_no] => 1
[weight] => 16230
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
[562] => stdClass Object
(
[id] => 562
[lot_no] => 2
[weight] => 15523
[staple] => 3600
[mic] => 0
[strength] => 0
[trash] => 0
[color_grade] => 0
)
)
答案 1 :(得分:0)
您还可以通过使用ID index
将结果存储到新数组中来获得所需的结果,例如:
<?php
$lot_details = $this->db->get()->result(); // your result
$newArray = array(); // initialize
foreach ($lot_details as $key => $value) {
$newArray[$value->id] = $value; // store in a new array
}
print_r($newArray);
?>