I'm still unable to get this code to work properly. I looked around for code that is similar what I'm trying to do. and I found this:
$inventory = array(
array("type"=>"fruit", "price"=>3.50),
array("type"=>"milk", "price"=>2.90),
array("type"=>"pork", "price"=>5.43),
);
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, $inventory);
print_r($inventory);
//output Array ( [0] => Array ( [type] => milk [price] => 2.9 ) [1] => Array ( [type] => fruit [price] => 3.5 ) [2] => Array ( [type] => pork [price] => 5.43 ) )
This code sorts the array by ascending order of the price value.
In my code I tried to adapt the code above:
$distances = array($event_id =>
array("event_name"=>$event_name,"distances"=>$distance)
);
$dist = array();
foreach ($distances as $key => $row)
{
$dist[$key] = $row['distances'];
}
var_dump($dist);
var_dump($distances);
array_multisort($dist, SORT_ASC,$distances);
echo "<br>";
print_r($distances);
out put:
////////vardump output for $dist:
array(1) { [1]=> float(135.23270826418) }
array(1) { [2]=> float(57.790571453271) }
array(1) { [3]=> float(135.81250714615) }
array(1) { [4]=> float(134.83200519242) }
array(1) { [5]=> float(135.68732962083) }
////////vardump output for $distance:
array(1) { [1]=> array(2) { ["event_name"]=> string(9) "Kotoricon" ["distances"]=> float(135.23270826418) } }
array(1) { [2]=> array(2) { ["event_name"]=> string(5) "I-CON" ["distances"]=> float(57.790571453271) } }
array(1) { [3]=> array(2) { ["event_name"]=> string(12) "LunaCon 2012" ["distances"]=> float(135.81250714615) } }
array(1) { [4]=> array(2) { ["event_name"]=> string(6) "Otakon" ["distances"]=> float(134.83200519242) } }
array(1) { [5]=> array(2) { ["event_name"]=> string(18) "New York comic Con" ["distances"]=> float(135.68732962083) } } */
//////print_r output
Array ( [0] => Array ( [event_name] => Kotoricon [distances] => 135.23270826418 ) )
Array ( [0] => Array ( [event_name] => I-CON [distances] => 57.790571453271 ) )
Array ( [0] => Array ( [event_name] => LunaCon 2012 [distances] => 135.81250714615 ) )
Array ( [0] => Array ( [event_name] => Otakon [distances] => 134.83200519242 ) )
Array ( [0] => Array ( [event_name] => New York comic Con [distances] => 135.68732962083 ) )
Please note above that all the top level Array[] have Array[0].
I'm not sure why the array is not being sorted since this the same technique used in the example I found above.
I noticed that when I add [$event_id] line to the foreach loop:
foreach ($distances[$event_id] as $key => $row)
{
$dist[$key] = $row['distances'];
//echo "key is ".$dist[$key]."<br>";
}
I get some very strange results
output:
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\otakufinder\scripts\geoloco.php on line 98
Array ( [1] => Array ( [event_name] => Kotoricon [distances] => 135.23270826418 ) )
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\otakufinder\scripts\geoloco.php on line 98
Array ( [2] => Array ( [event_name] => I-CON [distances] => 57.790571453271 ) )
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\otakufinder\scripts\geoloco.php on line 98
Array ( [3] => Array ( [event_name] => LunaCon 2012 [distances] => 135.81250714615 ) )
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\otakufinder\scripts\geoloco.php on line 98
Array ( [4] => Array ( [event_name] => Otakon [distances] => 134.83200519242 ) )
Warning: array_multisort() [function.array-multisort]: Array sizes are inconsistent in C:\xampp\htdocs\otakufinder\scripts\geoloco.php on line 98
Array ( [5] => Array ( [event_name] => New York comic Con [distances] => 135.68732962083 ) )
Questions:
1.a I get a warning and all the top level Arrays have unique indexes. Please let me know why The Top level Array sometimes does not get a index of 0? Is this the same array being overwritten in the foreach loop? Because of that, this is why the sort is failing?
1.b When you compare the vardump from distances and dist, the sizes are consistent. The warning can also be from a syntax error. However I'm not missing any commas or quotes. Can there be another reason for the warning?
- Is the array and foreach code written correctly? Do you know of another example I can follow?