Sho Gum Lew
Sho Gum Lew

Reputation: 339

Sorting associative array with multiple conditions

I have a table called "parents_kids", it has three columns known as:

id  Guardian    kids
1   Greg        1
2   Gary        3
3   Aaron       2
4   Farb        3
5   REX         1
6   Fred        2

In the following code I get it into array and print it:

$exc = $conn->prepare("SELECT ID,Guardian,kids from current_state");
        $exc->execute();
            while($finalResult = $exc->fetch(PDO::FETCH_ASSOC))
             {
                        $tables[] = $finalResult;
             }
var_dump($tables);

once i got this into an array, i sorted it in a custom order where first the guardians with 2 kids, then with 1 kid and who has 3

ID    Guardian  kids
3     Aaron     2
6     Fred      2
1     Greg      1
5     REX       1
4     Farb      3
2     Gary      3

Now my requirement is to order this rest of the array according to its ID to descending order,

for example just like this

ID    Guardian  kids
6     Fred      2
3     Aaron     2
5     REX       1
1     Greg      1
4     Farb      3
2     Gary      3

i tried this many times but its dint work, the code i used to sort the first requirement is as follows

$order = array("2", "1", "3");

usort($tables, function ($a, $b) use ($order) {
    $pos_a = array_search($a['kids'], $order);
    $pos_b = array_search($b['kids'], $order);
    return $pos_a - $pos_b;
});

can anybody help me to fullfil my second requirement which is sort the array by ID to descending order

This is how the array looks like

array (size=3)
  0 => 
    array (size=3)
      'ID' => string '3' (length=1)
      'Guardian' => string 'Aaron' (length=5)
      'kids' => string '2' (length=1)

array (size=3)
  1 => 
    array (size=3)
      'ID' => string '6' (length=1)
      'Guardian' => string 'FRED' (length=4)
      'kids' => string '2' (length=1) 

etc

P.S doing it from the table is not going to work, because im using a loop above the code which makes it impossible to do it from the SQL, can anybody tell me a way to do it from the php

Upvotes: 2

Views: 54

Answers (2)

merlin2011
merlin2011

Reputation: 75555

There were two primary issues.

  1. You were using status as the first key when it should have been kids based on your description.
  2. You need to check whether the first subtraction was zero, and if so, compare the id values.

Here is a working solution.

$order = array("2", "1", "3");

usort($tables, function ($a, $b) use ($order) {
    $pos_a = array_search($a['kids'], $order);
    $pos_b = array_search($b['kids'], $order);
    $result =  $pos_a - $pos_b;
    if ($result != 0) return $result;

    return intval($b['ID']) - intval($a['ID']);
});

var_dump($tables);

Upvotes: 0

mrosiu
mrosiu

Reputation: 71

I think that it should help:

select ID,Guardian,kids
from current_state
order by case WHEN kids = 2 then 1
              WHEN kids = 1 then 2
              WHEN kids = 3 then 3
              ELSE kids END ASC
, ID desc

Upvotes: 2

Related Questions