Search code examples
phpsummarydivide

PHP calculate total and split


How to calculate total and split on 4 without duplicate line with php? Example:

  • 27
  • 43
  • 57
  • 29
  • 12
  • 23
  • 44
  • 23
  • 52

I want to divide summary by 4 without duplicating line. My point is to have 4 persons with closest results.

Last few hours I'm looking in google to find something similar but I couldn't.

Thank You!


Solution

  • If I understand you correctly, I think this is what you want:

    sort($array); // Put the array into numeric order
    $group_by_4 = array_chunk($array, 4);
    

    array_chunk will return a 2-dimensional array, where each element is a group of 4 elements of the input array.

    Create $array by fetching the elements from the table into an array.

    while ($row = $conn->fetch_assoc()) {
        $array[] = $row['score'];
    }