我有一个保存以下公司记录的php数组。一旦创建-我想排列数组值或创建一个新的数组,所有列出的公司按距离排序(从小到大)。对如何完成这项工作有什么建议吗?
谢谢。
<?php
$company_values = array(
array(
'entitysymbol' => 'ANF',
'distance' => '0.42',
),
array(
'entitysymbol' => 'MSFT',
'distance' => '0.50',
),
array(
'entitysymbol' => 'GOOG',
'distance' => '0.78',
),
);发布于 2019-07-03 01:12:02
试一试:
function cmp($a, $b) {
if ($a['distance'] == $b['distance']) {
return 0;
}
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
usort($company_values,"cmp");
var_dump($company_values);有关更多信息,请阅读usort函数。希望我能帮上忙:)
发布于 2019-07-03 00:12:59
下面是一些代码,它们将执行您想要的操作:
<?php
// Define your array that needs sorting
$company_values = array(
array(
'entitysymbol' => 'MSFT',
'distance' => '0.60',
),
array(
'entitysymbol' => 'ANF',
'distance' => '0.42',
),
array(
'entitysymbol' => 'GOOG',
'distance' => '0.57',
),
array(
'entitysymbol' => 'MSFT',
'distance' => '0.50',
),
array(
'entitysymbol' => 'ANF',
'distance' => '0.30',
),
array(
'entitysymbol' => 'GOOG',
'distance' => '0.78',
),
array(
'entitysymbol' => 'MSFT',
'distance' => '0.50',
),
array(
'entitysymbol' => 'ANF',
'distance' => '0.42',
),
array(
'entitysymbol' => 'GOOG',
'distance' => '0.88',
)
);
// Function to initialise a, b, and c before we start our search
function refresh_dichotomy($array) {
$a = 0;
$b = sizeof($array) - 1;
$c = (int)(($a + $b) / 2);
return [$a, $b, $c];
}
// Recursive dichotomy function to close in on where we need to insert the next element
function get_next_indexes($a, $b, $c, $array, $key, $value) {
// If the value stored in the middle element is higher
if($array[$c][$key] > $value) {
// We lower the value of b
$b = $c;
// Otherwise
} else {
// We increse the value of a
$a = $c;
}
// If a and b are not adjacent
if($b - $a > 1) {
// Get the middle value
$c = (int)(($a + $b) / 2);
// And dig deeper
$a = get_next_indexes($a, $b, $c, $array, $key, $value);
}
return $a;
}
// Function to Fill the new array with the sorted values of the old one
function sort_array($old_array) {
// Add the first element
$new_array = Array($old_array[0]);
// Cycle through the remaining elements
for($i = 1; $i < sizeof($old_array); $i++) {
// First check if the next element is smaller than the smallest value
if($old_array[$i]['distance'] < $new_array[0]['distance']) {
// If it is, then put it in first place
array_splice($new_array, 0, 0, Array($old_array[$i]));
}
// Or if the next element is bigger than the biggest value
else if($old_array[$i]['distance'] > $new_array[count($new_array) - 1]['distance']) {
// If it is, then put it in last place
array_splice($new_array, count($new_array), 0, Array($old_array[$i]));
// Otherwise, find where we need to insert the next element
} else {
// Create 3 variables that will help us search via dichotomy
$a;
$b;
$c;
// Initialise their values to fit the current size of the new array
$init = refresh_dichotomy($new_array);
$a = $init[0];
$b = $init[1];
$c = $init[2];
// Find where we need to insert our next value
$a = get_next_indexes($a, $b, $c, $new_array, 'distance', $old_array[$i]['distance']);
// Insert our new element
array_splice($new_array, $a+1, 0, Array($old_array[$i]));
}
}
return $new_array;
}
// Create an array to store the sorted values
$sorted_company_values = array();
// Sort !
$sorted_company_values = sort_array($company_values);
// Preview before and after
print_r($company_values);
echo("<br><br>");
print_r($sorted_company_values);
?>这段代码创建了一个包含您的排序值的新数组,如果您不需要新数组,请替换:
$sorted_company_values = sort_array($company_values);通过
$company_values = sort_array($company_values);它基本上是通过dichotomy (more here)或binary search实现的recursive insertion sort
发布于 2019-07-03 01:58:31
正确答案:
usort($array, function ($a, $b)
{
if (abs($a['distance'] - $b['distance']) <= 0.0001)
{
return 0; # equal
}
return $a['distance'] <=> $b['distance']; # swap $a and $b here for DESC order
});我想强调的是,当您处理浮点值时,您不应该将它们像整数一样进行比较。主要是因为任何数字的浮点表示,例如0.1可以同时存储为0.0999999997或0.1000000003。为了避免这个问题,你应该引入"epsilon“变量(在这里等于0.0001),这样你就可以说”两个浮点数之间的差异小于epsilon,所以我们应该将它们作为相等的数字“。
更多信息可以在这里找到:Floating point numbers,请参阅带有“浮点精度”标题的警告。
https://stackoverflow.com/questions/30135005
复制相似问题