Home > other >  Grouping array of objects base on multiple property value of each object
Grouping array of objects base on multiple property value of each object

Time:11-13

I have an array of object of students from different schools and country. I want them to be grouped by name of school and country. How can I do the nested key value. I am new in programming and this one is hard for me. Can someone teach me on how to do this. thank youu.

Example:


//array of students
$students = [
  [
    “country” => “japan”,
    “school”  => “kyoto university”,
    “name”    => “Naruto Uzumaki”
  ],
  [
    “country” => “usa”,
    “school”  => “harvard university”,
    “name”    => “Naruto Uzumaki”
  ], 
  [
    “country” => “japan”,
    “school”  => “tokyo university”,
    “name”    => “sasuke Uchiha”
  ],
  [
    “country” => “japan”,
    “school”  => “tokyo university”,
    “name”    => “kakashi hatake”
  ],
];

Results:

[
  “japan” => [
      “kyoto university” => [
          [
            “country” => “japan”,
            “school”  => “kyoto university”,
            “name”    => “Naruto Uzumaki”
          ],
      ],

      “tokyo university” => [
          [
            “country” => “japan”,
            “school”  => “tokyo university”,
            “name”    => “sasuke Uchiha”
          ],
          [
            “country” => “japan”,
            “school”  => “tokyo university”,
            “name”    => “kakashi hatake”
          ],
      ],

   ],
   

   “usa” => [
       “harvard university” => [
           [
             “country” => “usa”,
             “school”  => “harvard university”,
             “name”    => “Naruto Uzumaki”
           ],
       ]
     ]

]

How can I grouped the students by country and school like you see in my “results”.

CodePudding user response:

Pretty much creating an object or using an existing one. First create country object if not exists, then create school object under it (if not exists) then push to it.

$result = [];
foreach ($students as $student) {
    $country = $student['country'];
    $school = $student['school'];
    $result[$country] = isset($result[$country]) ? $result[$country] : [];
    $result[$country][$school] = isset($result[$country][$school]) ? $result[$country][$school] : [];
    $result[$country][$school][] = $student;
}
print_r($result);

Output:

Array
(
    [japan] => Array
        (
            [kyoto university] => Array
                (
                    [0] => Array
                        (
                            [country] => japan
                            [school] => kyoto university
                            [name] => Naruto Uzumaki
                        )

                )

            [tokyo university] => Array
                (
                    [0] => Array
                        (
                            [country] => japan
                            [school] => tokyo university
                            [name] => sasuke Uchiha
                        )

                    [1] => Array
                        (
                            [country] => japan
                            [school] => tokyo university
                            [name] => kakashi hatake
                        )

                )

        )

    [usa] => Array
        (
            [harvard university] => Array
                (
                    [0] => Array
                        (
                            [country] => usa
                            [school] => harvard university
                            [name] => Naruto Uzumaki
                        )

                )

        )

)

CodePudding user response:

  • Loop over each row
  • Create convenient variables from the row's associative elements with extract()
  • Push the row into the nested associative structure using two of the three generated variables

Code: (Demo)

$result = [];
foreach ($students as $student) {
    extract($student);
    $result[$country][$school] = $student;
}
var_export($result);

It is not at all necessary to instantiate/declare parent elements before push child elements when using square brace pushing syntax. If you are using array_push(), the parent element must be declared in advance.

  • Related