Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deepu07's avatar
Level 11

sorting multidimensional array by one value

Hi Guys, My Api result is like this. Here I want to display students orderBy('start_year') in result. How can I achieve that. Any help that would be appreciated. Thanks!

  324 => array:2 [
    "subjects" => array:2 [
      0 => array:7 [
        "id" => "4"
        "name" => "abc"
        "type" => "Arts"
      ]
      1 => array:7 [
        "id" => "7"
        "name" => "ghi"
        "type" => "Music"
      ]
    ]
    "students" => array:35 [
      0 => array:11 [
        "course_id" => "19"
        "subject_number" => "30"
        "subject_name" => "Season 30"
        "start_year" => "2016"
        "end_year" => "2009"
      ]
      1 => array:11 [
        "course_id" => "196"
        "subject_number" => "31"
        "subject_name" => "Season 31"
        "start_year" => "2007"
        "end_year" => "2010"
      ]
      2 => array:11 [
        "course_id" => "5722343234"
        "subject_number" => "2018"
        "subject_name" => "Season 2018"
        "start_year" => "2018"
        "end_year" => "2018"
      ]
  ]
]```
0 likes
5 replies
bobbybouwmann's avatar

Laravel has collections which makes this really easy! So let's say you have your array here

$array = [ 
    'subjects' => [
        0 => [
            'id' => 4,
            'name' => 'abc',
            'type' => 'arts',
        ],
        // More data here
    ],
    'students' => [
        0 => [
            'course_id' => 19,
            'subject_number' => 30,
            'subject_name' => 'Season 30',
            'start_year' => 2006,   
            'end_year' => 2009,
        ],
        1 => [
            'course_id' => 196,
            'subject_number' => 31,
            'subject_name' => 'Season 31',
            'start_year' => 2007,   
            'end_year' => 2010,
        ],
        // More data here
    ],
];

// Make a collection from the students
$collection = collect($array['students']);

// Order them by the given field
// Each item is an array item of the "students" array
$collection->sortBy(function ($item) {
    return $item['start_year'];
});

// Convert the collection back to an array
$sortedArray  = $collection->all();

Documentation: https://laravel.com/docs/5.7/collections#method-sortby

Let me know if this works for you!

2 likes
deepu07's avatar
Level 11

@bobbybouwmann thanks for reply in my case $collection = collect($array[0]['students']); is giving back the result in students collection. But $collection->sortBy(function ($item) { return $item['start_year']; }); giving an err like this (1/1) ErrorException Illegal string offset 'start_year'

bobbybouwmann's avatar

MMh you can check what kind of value you get back in the callback. I assumed it would be an array, but it can be an object as well!

$collection->sortBy(function ($item) {
    dd($item); // What does this show? Based on this you can grab the correct value!
});
deepu07's avatar
Level 11

getting like this

[{"course_id":"423","subject_number":"30","subject_name":"Se 30","start_year":"2009","end_year":"2009"}, \n
 {"course_id":"424","subject_number":"31","subject_name":"Sea 31","start_year":"2010","end_year":"2010",}, \n
.......
bobbybouwmann's avatar

@deepu07 So it's a json object? Well that means you first need to decode it to an array to access it.

$array = json_decode($data);

Please or to participate in this conversation.