How to Sort Multidimensional Arrays Using PHP

preview_player
Показать описание

In this video, you'll learn how to use array_multisort as a basic method to sort multidimensional arrays in PHP; as well as, a custom function for more advanced sorting using a specified key.

-~-~~-~~~-~~-~-
Please watch: "Ryan Carson: How to Get an IT Job WITHOUT a College Degree"
-~-~~-~~~-~~-~-
#php #webdev
Рекомендации по теме
Комментарии
Автор

This is by far one of the most comprehensible and easy to understand videos that I've seen on this. And trust me, i've watched a lot over the past two days. Thank you.

marcomacaluso
Автор

The best video for multidimensional array sorting by so far.
Bless you!

diy-speaker
Автор

Thank you, that is exactly what i was looking for - for hours!

la_place
Автор

ohh man...i know this is an old video, but still THANK YOU!!!! you saved me lots of headache!!!! Cheers!!!

attilabalog
Автор

Very good tutorial! I could hear it well and clear, on topic the entire time and well thought out.

GilbertBigelow
Автор

Thank you so much, my friend. You have saved me a lot of headaches!

betogm
Автор

Thank you, so much... This simple function help me a lot...

marceloferreirafernandemar
Автор

Thank you so much! I had to make slight tweak since numeric indices surfaced 'offset' errors. This is the tweak - just in case someone is running into the same issue: $b[$k] and $c[$k].

rayvnm
Автор

this works great ... issue is it doesnt work with dates... because it will only look at the first number in the date.

kabengwapatu
Автор

Thank you very much!

GOD bless you.

georgigeorgiev
Автор

The best video on sorting arrays that I've found.. Thanks!
I'm having difficulties applying this to the data I'm provided. I have a 2nd layer in my array.
The sample data in the video is as follows:
<pre>Array
(
[0] => Array
(
[name] => John
[age] => 30
)

[1] => Array
(
[name] => Joe
[age] => 28
)


and my dataset has a [rows] element:
<pre>Array
(
[rows] => Array
(
[0] => Array
(
[category1] => Membership
[category2] => Camp FULL
[category3] =>
[description] =>
[end_date] => 2019-08-15




How can I modify the code to operate on the data which is one layer further into the dataset? I'll want to sort on the category 1, 2, 3, or end_date.


Thanks!

cdito
Автор

Pleas read more about array_multisort.
This is the fragment of code that I write when I needed to sort multidemensional array:

<?php
    $arr =     [
                ['lname' => 'Allower',          'fname' => 'Stewart',     'profesion' => 'cheef'],
                ['lname' => 'Kovalski',         'fname' => 'John',          'profesion' => 'programmist'],
                ['fname' => 'Savige',         'fname' => 'Allan',        'profesion' => 'accounter']
               ];
               foreach($arr as $key => $val)
               {
                   $lname[$key]         = $val['lname'];
                   $fname[$key]         = $val['fname'];
                   $profesion[$key]    = $val['profesion'];
               }
    array_multisort($lname, SORT_DESC, $fname, SORT_ASC, $arr );
    echo '<pre>';
    var_dump($arr);
    echo '</pre>';
Explenations:
In array_multisort you can set numbers of arrays to sort asc or desc as a column.
In last parameter you set the name of original array that you want to sort.
Important think is that the names of arrays shuld be the same that the collumn of multidemesional array.
So what is the point of building another function in script if you got build-in ready to use function?

JasiekH
welcome to shbcf.ru