Get the solution ↓↓↓
you can simply sort your parent array and then take your data from that array. lets say you have an array like
$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
),
);
now you want to sort that array using the total of the inner array. use array_multisort and array_column to do that.
array_multisort( array_column($data, "total"), SORT_DESC, $data );
this will generate an sorted array like
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
]
]
and now you have nothing more to do. just loop over the array and take values.
$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
];
you can use array data manipulationrsort() for des order andarray_slice() to take 5
public function dataStudentGraph($data)
{
$labels = [];
$graph_data = [];
foreach ($data['data'] as $student) {
$labels[] = $student['name'];
$graph_data[] = $student['total'];
}
$backupData = $graph_data;
rsort($graph_data); // des order
$sortedLabels = [];
foreach ($graph_data as $i => $data) {
$index = array_search($data, $backupData);
$sortedLabels[$i] = $labels[$index];
}
$array = [
[
'label' => 'Total Sales',
'color' => '#fec12c',
'data' => array_slice($graph_data, 0, 5) // take 5
]
];
$graph_data = [];
return [
'labels' => $sortedLabels,
'datasets' => $array
];
}
ref link
https://www.w3schools.com/php/php_arrays_sort.asp https://www.w3schools.com/php/func_array_slice.asp https://www.php.net/manual/en/function.array-search.php
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.