can we get the rows of data from desc and in array by use of rbxml?

you can use array data manipulation rsort() for des order and array_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

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top