I
I
ImVeryStupid2019-04-09 15:53:54
PHP
ImVeryStupid, 2019-04-09 15:53:54

Sorting output data?

The cycle works, displays on the page How to make an output sorted by the value of $var0, for example? Is it to write an array of arrays, sort and output?
echo $var0 . ' ' . $var1 . ' ' . $var2 . "<br>";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Russu, 2019-04-10
@ImVeryStupid

Do you need something like this or what?

The code
<?php

$arr = range(1, 200);
$arr[50] = 0;
$arr[100] = 0;
$arr[150] = 0;
$arr[200] = 0;
$arr[99] = 9999;

$preparedArray = prepareArray($arr);
$sortedArray = sortArray($preparedArray);
showArray($sortedArray);

function prepareArray(array $arr)
{
    $result = [];
    $counter = 0;
    $sum = 0;

    foreach ($arr as $key => $value) {
        if ($arr[$key] != 0) {
            $counter++;
            $sum += $value;
        } else {
            if ($counter != 0 and $sum / $counter > 20) {
                $result[] = [
                    'sum' => $sum,
                    'counter' => $counter,
                    'key' => $key,
                    'intval' => intval($key / 30 / 60),
                    'proc' => $key / 30 % 60,
                ];
            }

            $counter = 0;
            $sum = 0;
        }
    }

    return $result;
}

function sortArray(array $arr)
{
    usort($arr, function ($elem1, $elem2) {
        return $elem1['sum'] <=> $elem2['sum'];
    });

    return $arr;
}

function showArray(array $arr)
{
    foreach ($arr as $elem) {
        echo $elem['sum'] . '/' . $elem['counter'] . ' (' . $elem['key'] . ') ' . $elem['intval'] . ':' . $elem['proc'] . "<br>";
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question