2

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

5 Answers 5

3

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

1 Comment

I wanted 'hearts' to come after 'spades' in a normal array. I used this method with the first line: if (substr($a, 0, 1) == 'h') return strcmp('t' . substr($a, 1), $b); and the last line: return ($a < $b) ? -1 : 1; omitting all other lines.
3
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

3 Comments

thanks bro but i want to sort it through the array, using php
you should use the best tool is available, and not the one you decide to use: you should trust the experience of who you're asking, @ShoGumLew
I agree that sql is the best tool for this task. Unfortunately this answer (snippet dump) is missing its educational explanation.
1

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

1 Comment

You can change the numbers also in the sort array if u want to change the order later.
1

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.

Comments

1

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

Comments

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.