我试图为我的多维数组创建排序函数,但我无法弄清楚算法,。
以下是我要排序的数组的示例
[test1] => Array
(
[soldAvg] => 3
[inStock] => 100
)
[test2] => Array
(
[soldAvg] => 3
[inStock] => 0
)
[test3] => Array
(
[soldAvg] => 113
[inStock] => 31
)
[test4] => Array
(
[soldAvg] => 4
[inStock] => 1
)
[test5] => Array
(
[soldAvg] => 3
[inStock] => 1
)
我想按照soldAvg和inStock
之间的最大差异顺序对数组进行排序所以数组应该如下
[test1] => Array
(
[soldAvg] => 3
[inStock] => 100
)
[test3] => Array
(
[soldAvg] => 113
[inStock] => 31
)
[test4] => Array
(
[soldAvg] => 4
[inStock] => 1
)
[test2] => Array
(
[soldAvg] => 3
[inStock] => 0
)
[test5] => Array
(
[soldAvg] => 3
[inStock] => 1
)
我真正头痛的是,当inStock比销售平均值更大时,我不知道该怎么办
我唯一能接近工作的是
function usortAlgo($a,$b)
{
if($a['soldAvg']*2 / $a['inStock'] == $b['soldAvg']*2 / $b['inStock'])
return 0;
if($a['soldAvg']*2 / $a['inStock'] > $b['soldAvg']*2 / $b['inStock'])
return -1;
if($a['soldAvg']*2 / $a['inStock'] < $b['soldAvg']*2 / $b['inStock'])
return 1;
}
但是如果inStock比soldAvg更大它不起作用,如果soldAvg是0我得到这个错误&#34;除以零&#34;
答案 0 :(得分:1)
我知道已经有一段时间了,因为你发布了这个问题,但我最近做了类似于你所要求的事情,因为它可能会帮助其他访问者的堆栈流量:
/**
* function used in sortByDiff() sort the array
* @param mixed $a [description]
* @param mixed $b [description]
* @return int The comparison function must return an integer less than, equal to,
* or greater than zero if the first argument is considered to be
* respectively less than, equal to, or greater than the second.
*/
function cmp($a, $b) {
if ($a == $b)
return 0;
return ($a['diff'] < $b['diff']) ? -1 : 1;
}
/**
* sort an array in order of the diffence between to nested values
* @param array $toSort the array to sort
* @param String $valueName1 name of the first index
* @param String $valueName2 name of the second index
* @param boolean $rev true|false reverse the order if true
* @return array the sorted array
*/
function sortByDiff(array $toSort, $valueName1, $valueName2, $rev = false)
{
$sorted = $toSort;
foreach ($toSort as $key => $elem) {
$val1 = getValue($elem, array($valueName1));
$val2 = getValue($elem, array($valueName2));
if ($val1 === null || $val2 === null)
return null;
$sorted[$key]['diff'] = abs($elem[$valueName1] - $elem[$valueName2]);
}
uasort($sorted, 'cmp');
foreach ($sorted as $key => $elem)
unset($sorted[$key]['diff']);
if ($rev === true)
return array_reverse($sorted);
return $sorted;
}
/**
* @param array $array The array from which to get the value
* @param array $parents An array of parent keys of the value,
* starting with the outermost key
* @param bool $key_exists If given, an already defined variable
* that is altered by reference
* @return mixed The requested nested value. Possibly NULL if the value
* is NULL or not all nested parent keys exist.
* $key_exists is altered by reference and is a Boolean
* that indicates whether all nested parent keys
* exist (TRUE) or not (FALSE).
* This allows to distinguish between the two
* possibilities when NULL is returned.
*/
function &getValue(array &$array, array $parents, &$key_exists = NULL)
{
$ref = &$array;
foreach ($parents as $parent) {
if (is_array($ref) && array_key_exists($parent, $ref))
$ref = &$ref[$parent];
else {
$key_exists = FALSE;
$null = NULL;
return $null;
}
}
$key_exists = TRUE;
return $ref;
}
示例:强>
$toSort = [
'test1' => [
'soldAvg' => 3,
'intStock' => 100,
],
'test2' => [
'soldAvg' => 3,
'intStock' => 0,
],
'test3' => [
'soldAvg' => 113,
'intStock' => 31,
],
'test4' => [
'soldAvg' => 4,
'intStock' => 1,
],
'test5' => [
'soldAvg' => 3,
'intStock' => 1,
],
];
$sorted = sortByDiff($toSort, 'soldAvg', 'intStock', true);
print_r($sorted);
按预期出局:
Array
(
[test1] => Array
(
[soldAvg] => 3
[intStock] => 100
)
[test3] => Array
(
[soldAvg] => 113
[intStock] => 31
)
[test2] => Array
(
[soldAvg] => 3
[intStock] => 0
)
[test4] => Array
(
[soldAvg] => 4
[intStock] => 1
)
[test5] => Array
(
[soldAvg] => 3
[intStock] => 1
)
)