Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 选择数组中的值,分离并查询数据库laravel_Php_Mysql_Arrays_Laravel - Fatal编程技术网

Php 选择数组中的值,分离并查询数据库laravel

Php 选择数组中的值,分离并查询数据库laravel,php,mysql,arrays,laravel,Php,Mysql,Arrays,Laravel,我有数组中的ID,我需要获取所有ID,并根据选择在银行中使用每个ID进行选择,每个ID具有不同的回报。 我可以带来数字,但根据数组中记录的数量自动分离和使用select不起作用 以下是获取阵列的方法: 成绩表$学校 Array ( [0] => stdClass Object ( [id] => 1 [school_type_id] => 2 [external_id] =>

我有数组中的ID,我需要获取所有ID,并根据选择在银行中使用每个ID进行选择,每个ID具有不同的回报。 我可以带来数字,但根据数组中记录的数量自动分离和使用select不起作用

以下是获取阵列的方法:

成绩表$学校

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
           
        )

)
在控制器中,我只使用我想要的值,但我需要对它们进行选择,并根据每个值进行分离

Controller.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
)

        }*/
$schools=collect($api->schools);
$info=$schools->pull('id');

对于($i=0;$iwhere('sys_record.id_step'),“如果只需要在单独的数组/集合中获取
id
值,可以尝试该方法而不是for循环:


//将学校作为一个集合
$schools=collect($api->schools);
//取出所有的“id”键值
$info=$schools->pull('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);
}

$total = array_sum($information);

print_r( $total );

对于($i=0;$iwhere('sys\u record.id\u step'),当我计算这个值时,它返回我31,我把它放在一个数组中,但是我如何得到4而不是31?我相信它返回的是“3”和“1”,但你认为它们是“31”因为
print\u r
不会在结果之间添加任何空格或换行符。请尝试将
print\u r
移动到
for
的外部,然后查看得到的结果。我刚才看到了您的编辑。所以您需要获取数组中所有项的总和?如果是,在for之后只需执行
数组求和($informtation)
您将获得it@testCode查看最新的编辑。它可能会进一步优化您的代码
$schools = collect($api->schools);
$info = $schools->pluck('id');
$information = 0;

foreach( $info as $k => $i){
    $results_count = 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')
      ->count();

    $information += $results_count;
}

print_r( $information );
$schools = collect($api->schools);
$info = $schools->pluck('id');

$results_count = DB::table('sys_record')
      ->where('sys_record.id_step','<=',4)
      ->whereIn('sys_users.schoolId', $info)
      ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
      ->count();

print_r( $results_count );