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');
saikiran
  • 2,247
  • 5
  • 27
  • 42

3 Answers3

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);
Steve
  • 20,703
  • 5
  • 41
  • 67
  • You could do this directly in SQL as well, which would probably be the best option: http://stackoverflow.com/questions/14104055/ordering-by-specific-field-value-first – Steve Jan 12 '16 at 14:05
  • I know this but for this i need to write stored procedure to pass name as parameter. i don't want that. – saikiran Jan 12 '16 at 14:16
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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • i am wondering how to pass that madhapur to anonymous function in the usort. it is not static. – saikiran Jan 12 '16 at 13:47
  • `function (..) use ($name) { .. }`, where `$name` is a variable in the current scope which holds 'madhapur'. – deceze Jan 12 '16 at 13:47
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

Shailesh Singh
  • 317
  • 1
  • 11