Sho Gum Lew
Sho Gum Lew

Reputation: 339

Sort a php array in a custom order

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

id  owner   cars
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 name,state from current_state");
        $exc->execute();
            while($finalResult = $exc->fetch(PDO::FETCH_ASSOC))
             {
                        $tables[] = $finalResult;
             }
var_dump($tables);

once i get this into an array, is there a way i could sort it in a custom order where i could get the out put like follows, first the owners with 2 cars, then with 1 car and who has 3

owner   cars
Aaron   2
Fred    2
Greg    1
REX     1
Farb    3
Gary    3

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: 2575

Answers (5)

mhall
mhall

Reputation: 3701

You can use usort to sort the values. This will also sort by name if two owners have the same number of cars. I have changed the SELECT statement to match the given database definition.

$exc = $conn->prepare("SELECT owner, cars from current_state");
$exc->execute();

while ($finalResult = $exc->fetch(PDO::FETCH_ASSOC))
{
    $tables[] = $finalResult;
}

usort(
    $tables,
    function($a, $b) {
        // If same number of cars, sort by name
        if ($a['cars'] == $b['cars']) return strcmp($a['owner'], $b['owner']);

        // If owner a has two cars, place before b
        if ($a['cars'] == 2) return -1;

        // If owner b has two cars, place below a
        if ($b['cars'] == 2) return 1;

        // Neither owner a nor owner b has two cars, sort by number of cars
        return ($a['cars'] < $b['cars']) ? -1 : 1;
    }
);

foreach ($tables as $row) {
    echo $row['owner'], ' => ', $row['cars'], PHP_EOL;
}

Output:

Aaron => 2
Fred => 2
Greg => 1
REX => 1
Farb => 3
Gary => 3

Upvotes: 3

Adam
Adam

Reputation: 18875

Using PHP only, you can use uksort function to sort the array using a user-defined comparison function. The following code requires a copy of your $tables variable.

<?php
$tables2=$tables;
uksort($tables2, function($r1, $r2) use ($tables) {
    return ($tables[$r1]["cars"]%3) < ($tables[$r2]["cars"]%3);
});
print_r($tables2);

Upvotes: 1

Arun
Arun

Reputation: 101

Consider sorting the resultset through sql itself. The sql provided by @juergen would meet the purpose. The only change I would like to do in the query is that 'add owner field in the order by clause'. Consider the below code snippet

select * from car_owners
order by (case when cars = 2 then 1
              when cars = 1 then 2
              when cars = 3 then 3
              else 4
         end), owner

This should meet the purpose and give you the resultset exactly as you needed.

Also, if you explicitly need to sort it through php then you use the php usort() function and sort the array writing a custom defined function.

Upvotes: 1

Raj K
Raj K

Reputation: 371

If you have the array prepared from the mysql table, then you can use the following code-

$car_array=array(
    "Aaron"=>2,
    "Fred"=>2,
    "Greg"=>1,
    "REX"=>1,
    "Farb"=>3,
    "Gary"=>3,
    );

$sort_array=array("2","1","3");
$new_array=array();

foreach ($sort_array as $key => $value) 
{
    foreach ($car_array as $key1 => $value1) 
    {
        if ($value1 == $value ) 
            $new_array[$key1]=$value1;
    }   
}


print_r($new_array);

Upvotes: 1

juergen d
juergen d

Reputation: 204924

select * from your_table
order by case when cars = 2 then 1
              when cars = 1 then 2
              when cars = 3 then 3
              else 4
         end

Upvotes: 3

Related Questions