0

I'm trying to do something: I've got an Array that's layed out as such:

Array ( [0] => Nocciole con guscio 1000gr;;1;€5,50;0;0;No;0;
        [1] => Abbonamento Box Piccola - 4 Box;1;1;€60,00;0;0;Sì;4; 
        [2] => Box Media - Mista;2;1;€27,00;0;0;No;0; 
        [3] => La Tua Box - Piccola;1;1;€25,00;Mele, Pere;Finocchi, Radicchio Tondo, Cetrioli, Peperoni Rossi;No;0; 
        [4] => Abbonamento Box Piccola - 4 Box;1;1;€60,00;0;0;Sì;4; 
        [5] => Abbonamento Box Grande - 8 Box;3;1;€200,00;0;0;Sì;8; 
        [6] => Box Piccola - Mista;1;1;€20,00;0;0;No;0; 
        [7] => Abbonamento Box Media - 4 Box;2;1;€80,00;0;0;Sì;4; ) 

As you can see, all the values are formatted so that i can easily explode them into single arrays. Each one would look like this:

Subarray ( [0] => Abbonamento Box Piccola - 4 Box 
           [1] => 1
           [2] => 1 
           [3] => €60,00
           [4] => 0 
           [5] => 0 
           [6] => Sì 
           [7] => 4 
           [8] => )

What I need to do is sort the first array by Subarray[1] in descending order (note that index 1 contains a string) so that the Subarray keeps it's index=>value structure as originally was. Any clue?

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
  • `Subarray[1]` is not a string. It's an integer. Also it's empty for first "row". – Jakub Matczak Jan 26 '19 at 11:28
  • yes, but in the database is passed as a string that's why for some entries it is empty instead of 0. That shouldn't change much for the sorting, I don't care where the empty ones go, since I'm not really using them – Riccardo Ronconi Jan 26 '19 at 11:36

1 Answers1

0

Assuming your array name is $arr you can try something like this:

usort($arr, function($a, $b){
    $a = (int) explode(';', $a)[1];
    $b = (int) explode(';', $b)[1];

    if ($a === $b) {
        return 0;
    }

    return ($a < $b) ? 1 : -1;
});

Example on 3v4l

https://3v4l.org/Fm6vW

From PHP Manual:

https://secure.php.net/manual/en/function.usort.php

cn7r66
  • 164
  • 1
  • 5
  • Thanks a lot! That worked wonders, sorry if my question was a duplicate, but I didn't find anything while searching :) – Riccardo Ronconi Jan 26 '19 at 11:54
  • 1
    @RiccardoRonconi Note that it maybe pretty slow for big arrays as the `explode` needs to be done twice for each comparison. If you expect your input data to be large, I would recommend to optimize it by exploding your data a priori, then sort and in the end put it all back together. Also note that this answer assumes, the `Subarray[1]` is an integer although you said it's a string. Sorting by integers will give different results than sorting same numbers "alphabetically" when treated as strings. – Jakub Matczak Jan 26 '19 at 11:59
  • @JakubMatczak You are absolutely right, is better to explode the value before the sorting. I think that he meant that Subarray[1] is a string because of the explode function but as long as the value is numeric the (int) cast will do the job. – cn7r66 Jan 26 '19 at 12:06
  • Yeah, I just pointed it out, so the OP knows that it may depend on what he want to achieve. For some reasons he may expect the alphabetical order. – Jakub Matczak Jan 26 '19 at 12:07
  • @JakubMatczak Yes, it might be slow but it will never have to sort more than like 30 elements (it will only iterate through elements added to the database on 'today' that haven't been already processed). The problem is this is an extension of an already existing system that i'm not allowed to change, so i'll have to make due sadly. cn7r66 Yes, the value will always be numeric or empty. Thanks a lot guys! – Riccardo Ronconi Jan 26 '19 at 12:29