我在计算百分比后以这种方式返回值。
$school = $api->schools;
foreach ($school as $key => $value){
$name = $value->id;
$data = DB::table('sys_users')
->where('schoolid','=',$name)
->get();
$exist1 = count($data)*4;
$datarecord = DB::table('sys_record')
->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
->where('sys_users.schoolid','=',$name)
->get();
$correct1 = count($datarecord);
function porcentagem_nx($parcial,$total) {
return ($parcial * 100) / $total;
}
$worldone[$value->id] = porcentagem_nx($correct1, $exist1);
// Because you already used $data above, I chose a different name
// for this array.
$items[] = [
"id" => $value->id,
"name" => $value->name,
"worldone" => $worldone,
];
}
dd($items);
我将在数据表上使用它,因此我的手动测试有必要像下面的那样,但是是动态的。
$data = [
[
"id" => 1,
"name" => "Brand",
"worldone" => $worldone,
],
[
"id" => 2,
"name" => "Just",
"worldone" => $worldone,
],
];
我无法将 worldone 变量中的信息从 foreach 中取出并使变量 $school 中的所有数组记录的数组动态化。
SMILET