Javascript PHP:如何以desc顺序获取数据并在数组中显示数据?
我在数组中有数据,我想从数组中获取前5位的数据,并想在视图页面中显示。我对该函数的代码是:Javascript PHP:如何以desc顺序获取数据并在数组中显示数据?,javascript,php,laravel,Javascript,Php,Laravel,我在数组中有数据,我想从数组中获取前5位的数据,并想在视图页面中显示。我对该函数的代码是: public function dataStudentGraph($data) { $labels = []; $graph_data = []; foreach ($data['data'] as $student) { $labels[] = $student['name']; $grap
public function dataStudentGraph($data) {
$labels = [];
$graph_data = [];
foreach ($data['data'] as $student) {
$labels[] = $student['name'];
$graph_data[] = $student['total'];
}
$array = [
[
'label' => 'Total Student',
'color' => '#fec12c',
'data' => $graph_data
]
];
$graph_data = [];
return [
'labels' => $labels,
'datasets' => $array
];
}
我的查看页面代码是:
<div class="ibox-content">
{!! $student_graph !!}
</div>
我想按降序获取student['total']的前5个数据。请提供帮助。您可以使用数组数据操作rsort for des order和数组\u slice获取5 公共函数dataStudentGraph$data { $labels=[]; $graph_data=[]; foreach$data['data']作为$student{ $labels[]=$student['name']; $graph_data[]=$student['total']; } $backupdatea=$graph\u数据; rsort$graph_data;//des顺序 $sortedLabels=[]; foreach$graph_数据为$i=>$data{ $index=array\u search$data,$backupdatea; $sortedLabels[$i]=$labels[$index]; } $array=[ [ “标签”=>“总销售额”, “颜色”=>“fec12c”, 'data'=>array\u slice$graph\u data,0,5//取5 ] ]; $graph_data=[]; 返回[ “标签”=>$sortedLabels, “数据集”=>$array ]; } 参考链接
您只需对父数组进行排序,然后从该数组中获取数据即可。假设您有一个数组,如
$data = array(
array(
"name"=>"Urus",
"total"=>60
),
array(
"name"=>"Cayenne",
"total"=>85
),
array(
"name"=>"Bentayga",
"total"=>55
),
array(
"name"=>"Lamborghini",
"total"=>85
),
array(
"name"=>"Porsche",
"total"=>90
),
array(
"name"=>"Bentley",
"total"=>55
),
array(
"name"=>"Nissan",
"total"=>70
),
);
array:7 [▼
0 => array:2 [▼
"name" => "Porsche"
"total" => 90
]
1 => array:2 [▼
"name" => "Cayenne"
"total" => 85
]
2 => array:2 [▼
"name" => "Lamborghini"
"total" => 85
]
3 => array:2 [▼
"name" => "Nissan"
"total" => 70
]
4 => array:2 [▼
"name" => "Urus"
"total" => 60
]
5 => array:2 [▼
"name" => "Bentayga"
"total" => 55
]
6 => array:2 [▼
"name" => "Bentley"
"total" => 55
]
]
现在需要使用内部数组的总数对该数组进行排序。使用和来做这件事
array_multisort( array_column($data, "total"), SORT_DESC, $data );
这将生成一个排序数组,如
$data = array(
array(
"name"=>"Urus",
"total"=>60
),
array(
"name"=>"Cayenne",
"total"=>85
),
array(
"name"=>"Bentayga",
"total"=>55
),
array(
"name"=>"Lamborghini",
"total"=>85
),
array(
"name"=>"Porsche",
"total"=>90
),
array(
"name"=>"Bentley",
"total"=>55
),
array(
"name"=>"Nissan",
"total"=>70
),
);
array:7 [▼
0 => array:2 [▼
"name" => "Porsche"
"total" => 90
]
1 => array:2 [▼
"name" => "Cayenne"
"total" => 85
]
2 => array:2 [▼
"name" => "Lamborghini"
"total" => 85
]
3 => array:2 [▼
"name" => "Nissan"
"total" => 70
]
4 => array:2 [▼
"name" => "Urus"
"total" => 60
]
5 => array:2 [▼
"name" => "Bentayga"
"total" => 55
]
6 => array:2 [▼
"name" => "Bentley"
"total" => 55
]
]
现在你没什么事可做了。只需在数组上循环并获取值
$labels = $graph_data = [];
foreach ($data as $key => $student) {
if ($key < 5) {
$labels[] = $student['name'];
$graph_data[] = $student['total'];
} else {
break;
}
}
$array = [
[
'label' => 'Total Sales',
'color' => '#fec12c',
'data' => $graph_data
]
];
$graph_data = [];
return [
'labels' => $labels,
'datasets' => $array
];
我想按降序获取student['total']的前5个数据仍然不起作用,未定义的'sortDesc'@DaminiSuthar check显示错误现在我删除所有laravel集合现在纯数组函数给我排序数据,但标签与数据不符。他们都在不同的岗位上工作variables@DaminiSuthar现在检查我添加了$sortedLabels基于graph_数据数据数据是$student['total']是所有整数,或者还有一些其他格式,可能是数字或浮点