Home > Software engineering >  PHP Multi-dimensional array count and sum in table
PHP Multi-dimensional array count and sum in table

Time:10-03

My example is about a table with teachers, grades/groups and students generated with PHP. I would like to count the number of people in a grade per row (like in table on below). You can see the imagined generated table with results on below. I tried some solution with foreaches and for cycles but didn't work well. I don't understand the correct logics to earn the well cycle(s), so I don't have any idea how to solve it.

Can you help to guess the logic and write the working (e.g. foreach, for cycles) PHP code?

Here is my multidimensional array: (!! There are may be more people group in a grade like in Teacher 3 grade 1 array !!)

            $array = array (
                "teacher1" => array(
                    "grades" => array(
                        "grade 4" => array(
                            0 => 1,
                        ),
                    ),
                    "sum" => 1,
                    "name" => "teacher1",
                ),
                "teacher2" => array(
                    "grades" => array(
                        "grade 8" => array(
                            0 => 5,
                        ),
                        "grade 1" => array(
                            0 => 4,
                        ),
                    ),
                    "sum" => 9,
                    "name" => "teacher2",
                ),
                "teacher3" => array(
                    "grades" => array(
                        "grade 1" => array(
                            0 => 2,
                            1 => 1,
                        ),
                        "grade 4" => array(
                            0 => 1,
                        ),
                        "grade 5" => array(
                            0 => 1,
                        ),
                        "grade 2" => array(
                            0 => 1,
                        ),
                    ),
                    "sum" => 6,
                    "name" => "teacher3",
                ),
            );

The imagined table result:

enter image description here

Thank you for all helping!

CodePudding user response:

Hopefully this solution will helpful in your case.

foreach($array as $teacher)
{
    echo $teacher['name'];
    echo "<br>";

    $grades = $teacher['grades'];

    foreach($grades as $key => $grade)
    { 
        $count = 0;

        foreach($grade as $arr)
        {
            $count  = $arr;
        }
        
        echo "Total students in " . $key . " is = " . $count . "<br>";
    }
    
    echo "<br>";
}

CodePudding user response:

A big hairy data structure like this requires much planning in the beginning, and where you’ve run into trouble is in the innermost arrays. Instead of sticking with named arrays, you switched to indexed arrays, where the index is meaningless.

Your desired output is basically a flat table (which if it were a database table, should be normalized; then you wouldn’t be limited to 5 people per grade) where each of the first 32 columns is a unique field.

If your data structure looked like this, how much easier would it be:


$array = array (
     "teacher1" => array(
         "grades" => array(
             "grade 4" => array(
                 "1" => true,
             ),
         ),
         "sum" => 1,
         "name" => "teacher1",
     ),
     "teacher2" => array(
         "grades" => array(
             "grade 8" => array(
                 "5" => true,
             ),
             "grade 1" => array(
                 "4" => true,
             ),
         ),
         "sum" => 9,
         "name" => "teacher2",
     ),
     "teacher3" => array(
         "grades" => array(
             "grade 1" => array(
                 "2" => true,
                 "1" => true,
             ),
             "grade 4" => array(
                 "1" => true,
             ),
             "grade 5" => array(
                 "1" => true,
             ),
             "grade 2" => array(
                 "1" => true,
             ),
         ),
         "sum" => 6,
         "name" => "teacher3",
     ),
 );

Now, each of the first 32 cells of your table have a distinct name, i.e.,

if(isset($array['teacher2']['grades']['grade1']['4'])) { echo ‘x'; }

The problem, of course, is this is rather verbose to type out. You would naturally want to use foreach loops to dig down into the data structure, and you could have counting arrays for each of the "number of people" keys you encounter… but yikes!

If this is s project for school, then press ahead; most school courses don’t address practical ways to do things. In the real world, you’d use a database instead of this unwieldy array.

  • Related