0

I need to sort the array below by show_name ASC and DESC, I tried many methods suggested in stackoverflow website but no luck so far, please help

$mainArray :

[78] => Array ( 
[main] => Array ( 
    [season_num] => 1 
    [episode_name] => Workflow 1 
    [episode_num] => 2 
    [show_id] => 15 
    [show_name] => Workflow Test 
    [type] => 
    [by_promo] => 
    [promo_type] => 
    [status] => I 
    [terminated] => ) 
    [approved] => Array ( 
    [asset] => on 
    [promo] => 
    [ingest] => 
    [review] => 
    [editing] => 
    [quality_control] => 
    [mcr] => 
    [archive] => ) 

    [notes] => Array ( 
        [promo] => 
        [ingest] => 
        [review] => 
        [editing] => 
        [quality_control] => 
        [mcr] => 
        [archive] => 
        [terminated] => )

    [file_path] => Array ( 
        [ingest] => 
        [editing] => ) 
        [priority] => 2 
        [work_type] => 0 
        [c_breaks] => 
        [editing_staff] => Array (
        [assigned_to] => 
        [assigned_from] => 21 ) 
        [asset] => Array ( 
        [date] => 1394828762 
        [user] => 26 
        [fullname] => User 2 ) 

    [ingest] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [promo] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [review] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [editing] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [quality_control] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [mcr] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [archive] => Array ( 
        [date] => 
        [user] => 
        [fullname] => ) 

    [status] => pending ) 


[79] => Array ( 
    [main] => Array ( 
        [season_num] => 2 [episode_name] => Doctor Who test [episode_num] => 4 [show_id] => 14 [show_name] => Doctor Who [type] => [by_promo] => [promo_type] => [status] => I [terminated] => ) [approved] => Array ( [asset] => on [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => ) [notes] => Array ( [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => [terminated] => ) [file_path] => Array ( [ingest] => [editing] => ) [priority] => 2 [work_type] => 0 [c_breaks] => [editing_staff] => Array ( [assigned_to] => [assigned_from] => 21 ) [asset] => Array ( [date] => 1394828776 [user] => 26 [fullname] => User 2 ) [ingest] => Array ( [date] => [user] => [fullname] => ) [promo] => Array ( [date] => [user] => [fullname] => ) [review] => Array ( [date] => [user] => [fullname] => ) [editing] => Array ( [date] => [user] => [fullname] => ) [quality_control] => Array ( [date] => [user] => [fullname] => ) [mcr] => Array ( [date] => [user] => [fullname] => ) [archive] => Array ( [date] => [user] => [fullname] => ) [status] => pending ) 

[80] => Array ( 
    [main] => Array ( 
        [season_num] => 4 [episode_name] => Spooks Test [episode_num] => 6 [show_id] => 11 [show_name] => Spooks [type] => [by_promo] => [promo_type] => [status] => I [terminated] => ) [approved] => Array ( [asset] => on [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => ) [notes] => Array ( [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => [terminated] => ) [file_path] => Array ( [ingest] => [editing] => ) [priority] => 2 [work_type] => 0 [c_breaks] => [editing_staff] => Array ( [assigned_to] => [assigned_from] => 21 ) [asset] => Array ( [date] => 1394828792 [user] => 26 [fullname] => User 2 ) [ingest] => Array ( [date] => [user] => [fullname] => ) [promo] => Array ( [date] => [user] => [fullname] => ) [review] => Array ( [date] => [user] => [fullname] => ) [editing] => Array ( [date] => [user] => [fullname] => ) [quality_control] => Array ( [date] => [user] => [fullname] => ) [mcr] => Array ( [date] => [user] => [fullname] => ) [archive] => Array ( [date] => [user] => [fullname] => ) [status] => pending ) 

[81] => Array ( 
    [main] => Array ( 
        [season_num] => 6 [episode_name] => Anc test [episode_num] => 8 [show_id] => 19 [show_name] => Anc Test [type] => [by_promo] => [promo_type] => [status] => I [terminated] => ) [approved] => Array ( [asset] => on [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => ) [notes] => Array ( [promo] => [ingest] => [review] => [editing] => [quality_control] => [mcr] => [archive] => [terminated] => ) [file_path] => Array ( [ingest] => [editing] => ) [priority] => 2 [work_type] => 0 [c_breaks] => [editing_staff] => Array ( [assigned_to] => [assigned_from] => 25 ) [asset] => Array ( [date] => 1394829087 [user] => 26 [fullname] => User 2 ) [ingest] => Array ( [date] => [user] => [fullname] => ) [promo] => Array ( [date] => [user] => [fullname] => ) [review] => Array ( [date] => [user] => [fullname] => ) [editing] => Array ( [date] => [user] => [fullname] => ) [quality_control] => Array ( [date] => [user] => [fullname] => ) [mcr] => Array ( [date] => [user] => [fullname] => ) [archive] => Array ( [date] => [user] => [fullname] => ) [status] => pending ) 
1

1 Answer 1

0

To sort an array by the inner key you can usually use the usort function.

In this case your code would look something like this.

EDIT: Looks like you need another level. Try this.

usort($array, function ($a, $b) { return $b['main']['show_name'] < $a['main']['show_name']; });

To see this code in action, you can view here https://ideone.com/Mmv1gV

Keep in mind this actually modifies the value of your array in place. See http://php.net/usort for more information.

Another potentially helpful link I found, sorting array based on inner-array key-value

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the suggestion, but I tried many usort methods including yours, it still doesn't work.
@ancogenli This works for me, I have edited my answer with a link to running PHP code.
In your example there is two nested Arrays, in my question there are 3 nested Arrays, this might be the issue, still not working for me
Your structure has two until the part you want to sort... [78] => Array ( [main] => Array ( [season_num] => 1 [episode_name] => Workflow 1 [episode_num] => 2 [show_id] => 15 [show_name] => Workflow Test .. Possibly you are implementing the sorting incorrectly? And even if that is the case, simply add another part to the sorting function for the next array?
Edited my answer, try that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.