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