CI4 provides in built array functions for grouping and sorting array elements to some extent. Lets us look at two functions here
array_group_by and array_sort_by_multiple_keys
- array_group_by : This function takes in 3 parameters ; the input array, grouping conditions and a boolean value indicating whether we should include empty values. Let us look at an example. Below is a sample array of student data. We will group them by the subject name and rank. (Note that this function is available only for CI4 version 4.4.0 and above)
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
helper('array');
$students = [
0 => [
'name' => 'Mark',
'rank' => 3,
'subject' => [
'name' => "Calculus",
],
],
1 => [
'name' => 'Mathew',
'rank' => 5,
'subject' => [
'name' => "Calculus",
],
],
2 => [
'name' => 'Linda',
'rank' => 5,
'subject' => [
'name' => "Science",
],
],
3 => [
'name' => 'Liam',
'rank' => 1,
'subject' => [
'name' => "Science",
],
],
4 => [
'name' => 'Lester',
'rank' => 1,
'subject' => [
'name' => "Calculus",
],
],
5 => [
'name' => 'Ben',
'rank' => null,
'subject' => [
'name' => "Calculus",
],
],
6 => [
'name' => 'Sam',
'rank' => 3,
'subject' => [
'name' => "Calculus",
],
]
];
$result = array_group_by($students, ['subject.name', 'rank']);
print_r($result);
}
}
?>
We can use the dot notation to access the nested elements. The max depth allowed is 2. If we look at the output we see that the data is grouped on the basis of subject name first and then the rank. The value where rank is null is not considered here as we have not mentioned the third parameter. If we pass the third parameter as true, then the empty value is also considered during grouping. The output for above code is as follows:
Array
(
[Calculus] => Array
(
[3] => Array
(
[0] => Array
(
[name] => Mark
[rank] => 3
[subject] => Array
(
[name] => Calculus
)
)
[1] => Array
(
[name] => Sam
[rank] => 3
[subject] => Array
(
[name] => Calculus
)
)
)
[5] => Array
(
[0] => Array
(
[name] => Mathew
[rank] => 5
[subject] => Array
(
[name] => Calculus
)
)
)
[1] => Array
(
[0] => Array
(
[name] => Lester
[rank] => 1
[subject] => Array
(
[name] => Calculus
)
)
)
)
[Science] => Array
(
[5] => Array
(
[0] => Array
(
[name] => Linda
[rank] => 5
[subject] => Array
(
[name] => Science
)
)
)
[1] => Array
(
[0] => Array
(
[name] => Liam
[rank] => 1
[subject] => Array
(
[name] => Science
)
)
)
)
)
2. array_sort_by_multiple_keys : We can sort the array elements using this function . It takes two parameters ie. the input array and the sort columns. Let us consider the same array in the previous example and apply this function.
array_sort_by_multiple_keys($students, [
'subject.name' => SORT_ASC,
'rank' => SORT_ASC
]);
print_r($students);
We have added the parameters to sort based on subject.name first and then sort based on rank. So the output will be as below. The value with rank null will be on the top for this case and it is considered value 0.
Array
(
[0] => Array
(
[name] => Ben
[rank] =>
[subject] => Array
(
[name] => Calculus
)
)
[1] => Array
(
[name] => Lester
[rank] => 1
[subject] => Array
(
[name] => Calculus
)
)
[2] => Array
(
[name] => Mark
[rank] => 3
[subject] => Array
(
[name] => Calculus
)
)
[3] => Array
(
[name] => Sam
[rank] => 3
[subject] => Array
(
[name] => Calculus
)
)
[4] => Array
(
[name] => Mathew
[rank] => 5
[subject] => Array
(
[name] => Calculus
)
)
[5] => Array
(
[name] => Liam
[rank] => 1
[subject] => Array
(
[name] => Science
)
)
[6] => Array
(
[name] => Linda
[rank] => 5
[subject] => Array
(
[name] => Science
)
)
)