0

I would like to sort an array of arrays depent on a specific value in php.i do a lot of search and I used array_multisort() method. My problem is that sometimes the array is not sorted right.My questions are:

1.array_multisort() is the right method or is better to use usort()

  1. if the size of array is too big should i wait more time for the next code to run(the code that uses the sorted array). I am asking that because I have the problem when array is bigger (e.g more than 5 elements).

Here is my php code.

require_once __DIR__ . '/db_connect_win.php';

$db = new DB_CONNECT();

$alldrivers = array();

$result = mysql_query("SELECT *FROM temp") or die (mysql_error());

if(mysql_num_rows($result) > 0){

  while($row = mysql_fetch_array($result)){

    $driver = array();

    $driver["id"] = $row["id"];

    $driver["distance"] = $row["distance"];
    $driver["gcm_reg_id"] = $row["gcm_reg_id"];

    array_push($alldrivers,$driver);
  }

  if(count($alldrivers) > 0){

    $sortarray = array();

    foreach($alldrivers as $onedriver){

      foreach($onedriver as $key=>$value){

        if(!isset($sortarray[$key])){
          $sortarray[$key] = array();
        }

        $sortarray[$key][] = $value;

      }
    }
    $orderby = "distance";

    array_multisort($sortarray[$orderby],SORT_ASC,$alldrivers);

    include_once './GCM.php';

    $gcm = new GCM();

    $registation_ids = array($alldrivers[0]["gcm_reg_id"]);
    $message = array("callorcancel"=>"get_yes");
    $gcm_result = $gcm -> send_notification($registation_ids,$message);


    echo  $alldrivers[0]["id"];

  }
}

mysql_query('TRUNCATE TABLE temp;');

The problem is tha in the sorted array the element with index 0 must be the driver with the least distance value but sometimes its the one with the highest

Kyle Smith
  • 2,300
  • 1
  • 17
  • 10
Dimitris
  • 237
  • 1
  • 5
  • 11
  • 5
    why not sort the data in `SELECT * FROM temp order by some_col` and push to array instead of re arranging the array ? – Abhik Chakraborty Mar 14 '14 at 13:55
  • http://stackoverflow.com/questions/15062210/how-do-i-sort-the-follwing-array/15062573#15062573. – Prasanth Bendra Mar 14 '14 at 13:58
  • Always indent your code, so that others can read it – Prasanth Bendra Mar 14 '14 at 14:00
  • @AbhikChakraborty If I undestand correctly doing that the first element(driver) in the array will be the one with the highest value of distance.And I want the array to be sorted from low to high. – Dimitris Mar 14 '14 at 14:10
  • in sql you can do the low to high by a column just by adding `order by col_name` if u need to do highest to lowest u can do as `order by col_name desc` does it explains ? – Abhik Chakraborty Mar 14 '14 at 14:12
  • Happy to learn new thinks!:)So when I am running the `mysql_fetch_array($result)` it will create the sorted array.Am I right? – Dimitris Mar 14 '14 at 14:21
  • 1
    Yes the result set will be already sorted you can just add them in the array for further operation. The new array will be sorted in ascending or descending order depending on how u do order by :-) – Abhik Chakraborty Mar 14 '14 at 14:29
  • @AbhikChakraborty thank you very much for your awnser and your time!:) – Dimitris Mar 14 '14 at 21:04

2 Answers2

0

Try this :

$sort = array();
foreach($alldrivers as $k=>$v) {
    $sort['distance'][$k] = $v['distance'];
}

array_multisort($sort['distance'], SORT_DESC, $alldrivers);


echo "<pre>";
print_r($alldrivers);

Ref: http://php.net/manual/en/function.array-multisort.php

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
0
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well. i.e it is sorting index's. In first array 0 was on 4 pos and came to 1th place similarly in second array 4 pos comes at 1st pos.

THATS WHY U ARE GETTING WRONG ANS

Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36