2

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

3
  • Can you edit your post and add how the array looks like? Commented Mar 2, 2015 at 9:11
  • Why not just put an ORDER BY on your query and have the SQL server return the data in the order you want? Commented Mar 2, 2015 at 9:16
  • Theres a bigger code behind this, records get added one by one using the sql, therefore the ORDER BY does not apply to it, thats why i have got it into array and sorting it Commented Mar 2, 2015 at 9:24

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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);

3 Comments

@ShoGumLew, If this does not work you'll have to edit your example, since I tested on your example. :)
no need :D this wokrs like a charm... btw can you tell me somewhere a tute or something to practice these type of sorting with "use" keyword
@ShoGumLew, I don't know any sites that give comparator specific practice, but if you want to improve your algorithms you can look on Topcoder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.