使用零值对数组进行排序最后使用array_multisort

时间:2018-06-04 20:49:02

标签: php arrays sorting

我试图对一些学生进行排序'升级总分和降级总分,使用array_multisort工作除外,我有一个挑战,一个零总成绩和分数最高的学生。我需要学生在名单上排在最后。

    $vc_array=array(
      0 => 
        array(
              'student_name' =>  'student one',
              'total' =>  227,
              'total_grades' =>  27),
        1 => 
        array(
              'student_name' =>  'student two',
              'total' =>  313,
              'total_grades' =>  14),
        2 => 
        array(
              'student_name' =>  'student three',
              'total' =>  317,
              'total_grades' =>  13),
        3 => 
        array (
              'student_name' =>  'student four' ,
              'total' =>  271,
              'total_grades' =>  20),
        4 => 
        array (
              'student_name' =>  'student five',
              'total' =>  0,
              'total_grades' =>  0));


    foreach ($vc_array as $key => $row)
{
   $vc_array_value[$key] = $row['total_grades'];
   $vc_array_name[$key] = $row['total'];
 }
array_multisort($vc_array_value, SORT_ASC, $vc_array_name, SORT_DESC, vc_array);

当我运行var_dump($ vc_array);把它拿出来......

array (size=5)
  0 => 
    array (size=3)
      'student_name' => string 'student five' (length=12)
      'total' => int 0
      'total_grades' => int 0
  1 => 
    array (size=3)
      'student_name' => string 'student three' (length=13)
      'total' => int 317
      'total_grades' => int 13
  2 => 
    array (size=3)
      'student_name' => string 'student two' (length=11)
      'total' => int 313
      'total_grades' => int 14
  3 => 
    array (size=3)
      'student_name' => string 'student four' (length=12)
      'total' => int 271
      'total_grades' => int 20
  4 => 
    array (size=3)
      'student_name' => string 'student one' (length=11)
      'total' => int 227
      'total_grades' => int 27

1 个答案:

答案 0 :(得分:0)

我认为解决这个问题的一种方法是filter total total_grades$vc_array$vc_array为0的值,并将它们存储在单独的数组中。

然后执行排序,最后将两个值为零的数组合并到$totalZero = []; $vc_array = array_filter($vc_array, function($x) use (&$totalZero) { if ($x['total'] === 0 && $x['total_grades'] === 0) { $totalZero[] = $x; } return $x['total'] !== 0 || $x['total_grades'] !== 0; }); foreach ($vc_array as $key => $row) { $vc_array_value[$key] = $row['total_grades']; $vc_array_name[$key] = $row['total']; } array_multisort($vc_array_value, SORT_ASC, $vc_array_name, SORT_DESC, $vc_array); $vc_array = array_merge($vc_array, $totalZero);

 const multer = require('multer');
const upload = multer();

app.post('/api/sendFilesToServer',upload.fields([]), function (req, res) { 
const payload = req.body; 

console.log("files", payload.files[0]); 
console.log("metadata", payload.metadata); 

let formdata = new FormData(); 

//formdata.append("metadata",payload.metadata) 
formdata.append("metadata",'{"operationType":"Bulk","supportingDocuments":{"documents":[{"type":"photo","document":"Screenshot (1).png"}]}}')

formdata.append("files",payload.files[0]) 

axios({ 
method: 'POST', 
data: formdata,
url: `${url}/uploadFiles`, 
headers: headersPayload 
}).then(function (response) { 
console.log("Reponse form",req.body) 
console.log("RESPONSE") 
res.status(response.status).send(response.data); 
}).catch(function (error) { 
console.log("Reponse form ERROR",req.body) 
console.log("ERROR",error.response.data.message) 
res.status(error.response.status).send(error.response.data.message) 
});

Demo