2

I have a php array coming as result set from database.

as i am less familiar with mySQL sorting, i want to sort my result set based on the a particular string say "madhapur"

$arr=array(    
    array("name" => 'madhapur',"population" =>'1000'),
    array("name" => 'jubiliee hills',"population" =>'800'),
    array("name" => 'madhapur',"population" =>'900'),
    array("name" => 'adikmet',"population" =>'200'),
    array("name" => 'sr nagar',"population" =>'3000'),
    array("name" => 'jubilee hills',"population" =>'1200'),
    array("name" => 'madhapur',"population" =>'1000')   
    );

I am expecting the result as below

$arr=array(
array("name" => 'madhapur',"population" =>'1000'),
array("name" => 'madhapur',"population" =>'1000'),
array("name" => 'madhapur',"population" =>'900'),
array("name" => 'adikmet',"population" =>'200'),
array("name" => 'jubilee hills',"population" =>'1200'),
array("name" => 'jubiliee hills',"population" =>'800'),
array("name" => 'sr nagar',"population" =>'3000'),
);

I tried using usort but all of them are used to sort either descending or ascending.

function sortByName($a,$b){
         return $b['name'] - $a['name'];
    }
usort($arr,'sortByName');
3
  • So the only constraint is, arrays with name as madhapur should come at the beginning, right? Commented Jan 12, 2016 at 13:35
  • Why not sort it by MySQL? should be faster Commented Jan 12, 2016 at 14:05
  • to sort using mySQL i need to write stored procedure unless i can't pass name as parameter. Commented Jan 12, 2016 at 14:17

3 Answers 3

2

First order by name and population in your sql, as your example appears to show everything ordered in that manner apart from the specific name (madhapur) promoted to the top

(note DB class is an example because i dont know what db access methods you use):

$arr = DB::Query("SELECT name, population 
                  FROM tablename 
                  ORDER BY name ASC, population DESC");

then you can iterate the array, and place the elements with the name you want at the beginning:

function nameAtFront($arr, $name){

    $copy = $arr;
    $total = count($arr);
    while($total--){
        if($copy[$total]['name']==$name){
            unset($arr[$total]);
            array_unshift($arr, $copy[$total]);
        }
    }
    return $arr;
}
$out = nameAtFront($arr, 'madhapur');
var_dump($out);
Sign up to request clarification or add additional context in comments.

2 Comments

You could do this directly in SQL as well, which would probably be the best option: stackoverflow.com/questions/14104055/…
I know this but for this i need to write stored procedure to pass name as parameter. i don't want that.
1

To sort the name madhapur to the top and otherwise leave the order of elements as is/random:

usort($arr, function (array $a, array $b) {
    $a = $a['name'] == 'madhapur';
    $b = $b['name'] == 'madhapur';

    if ($a == $b || (!$a && !$b)) {  // both equal or both not madhapur
        return 0;
    } else if ($a) {
        return -1;
    } else {
        return 1;
    }
});

But this is really something that should be done in SQL.

2 Comments

i am wondering how to pass that madhapur to anonymous function in the usort. it is not static.
function (..) use ($name) { .. }, where $name is a variable in the current scope which holds 'madhapur'.
0

I hope this will help you

function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;
}

for more detail take reference from Sort Multi-dimensional Array by Value

1 Comment

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.