【发布时间】:2020-11-08 12:37:25
【问题描述】:
我有通过数组传入的 id,我需要将它们全部获取并在银行中进行选择,根据选择使用具有不同回报的每一个。 我可以带上数字,但无法根据数组中的记录数自动分离和使用选择。
下面是我获取数组的方式:
结果数组 $schools
Array
(
[0] => stdClass Object
(
[id] => 1
[school_type_id] => 2
[external_id] =>
[name] => School1
[active] => 1
[created_at] => 2020-02-21 23:42:11
[updated_at] => 2020-02-21 23:42:11
[deleted_at] =>
[active_text] => Ativo
)
[1] => stdClass Object
(
[id] => 81
[school_type_id] => 2
[external_id] =>
[name] => School2
[active] => 1
[created_at] => 2015-05-27 18:08:52
[updated_at] => 2015-05-27 18:08:52
[deleted_at] =>
[active_text] => Ativo
)
)
在我使用的控制器中,我只带入我想要的值,但我需要对它们进行选择并根据每个值分开。
控制器.php
$schools = collect($api->schools);
$info = $schools->pluck('id');
for($i = 0;$i<count($info);$i++){
$select = DB::table('sys_record')
->where('sys_record.id_step','<=',4)
->where('sys_users.schoolId','=',$info[$i])
->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
->get();
$information[] = count($select);
print_r($information);
/*return Array
(
[0] => 3
)
Array
(
[0] => 3
[1] => 1
)
}*/
数组 31 正在返回,我需要 4 个
【问题讨论】:
-
我相信它会返回“3”和“1”,但您将它们视为“31”,因为
print_r不会在结果之间添加任何空格或换行符。尝试将print_r移到for之外,看看你会得到什么 -
我刚看到你的编辑。所以你需要得到数组中所有条目的总和吗?如果是,则在 for 之后只需执行
array_sum( $informtation )即可获得它