php - 使用键中的 id 而不是数组键中的 0,1 获取数组结果
问题描述
我使用下面的查询来获取以 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
)
)
任何人都可以为这个查询问题提供解决方案吗?
解决方案
这是一个班轮,
// null will take id as key and whole array as value
$temp = array_column($temp, null, "id");
参考:array_column。
演示。
输出:
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
)
)
推荐阅读
- sql - 通过 shell 脚本执行合并时出错
- deep-learning - YOLO3 不能在 GTX 2080 TI 上运行?
- multithreading - 如何将期货中的函数输出传递给新函数?
- python - 通过定义的哈希调用函数,而不是 python 中的愚蠢的人类给定的名称
- cython - .pyd 和 .pyx 文件中声明的行为差异
- python - 场景所需的解决方案
- php - 如何根据经纬度应用过滤器?
- azure - 如何使用 Azure SignalR 服务托管 SignalR 集线器?
- python - 如何解决 AttributeError:SMTP_SSL 实例在 Python 中没有属性“__exit__”?
- java - java FX - 日历中的 Datepicker 天数变灰