Php 对多维产品数组进行排序的逻辑
我必须根据价格或大小对多维数组进行排序,但问题是,它会处理一个问题,它排序的第一个数组总是错误的,我尝试使用usort,等等,但它们都给出了相同的结果,我们有一个需要根据价格或大小进行排序的产品列表。我找到了很多答案,但问题还是一样。产品应根据价格进行分类,即从低到高,或者尺寸将根据宽度进行分类,例如产品尺寸120 x 60,因此120是宽度,应该是最后一个产品 函数对多维数组进行排序Php 对多维产品数组进行排序的逻辑,php,arrays,sorting,Php,Arrays,Sorting,我必须根据价格或大小对多维数组进行排序,但问题是,它会处理一个问题,它排序的第一个数组总是错误的,我尝试使用usort,等等,但它们都给出了相同的结果,我们有一个需要根据价格或大小进行排序的产品列表。我找到了很多答案,但问题还是一样。产品应根据价格进行分类,即从低到高,或者尺寸将根据宽度进行分类,例如产品尺寸120 x 60,因此120是宽度,应该是最后一个产品 函数对多维数组进行排序 function array_sort($array, $on, $order=SORT_ASC){ $
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array; }
这是我要排序的数组
$sortingArray = Array(
'0' => Array(
'id' => 1287,
'data-price' => '£209.95',
'size' => '120 x 60'
),
'1' => Array(
'id' => 1263,
'data-price'=> '£14.95',
'size' => '18 x 13'
),
'2' => Array(
'id' => 1264,
'data-price' => '£15.95',
'size' => '20 x 15'
),
'3' => Array(
'id' => 1245,
'data-price' => '£29.95',
'size' => '30 x 20'
),
'4' => Array
(
'id' => 1315,
'data-price' => '£39.95',
'size' => '50 x 13'
),
'5' => Array(
'id' => 1275,
'data-price' => '£94.95',
'size' => '60 x 40'
),
'6' => Array
(
'id' => 1281,
'data-price' => '£119.95',
'size' => '75 x 50'
),
);
输出结果
$rectangleProductDetails = array_sort($sortingArray, 'size', SORT_ASC);
print_r($rectangleProductDetails);
您需要将大小转换为int 也许像
usort($sortingArray, function($a, $b) {
//you can calculate your size and sort the result
//or split it into 2 number and then sort it
return intval($a["size"]) < intval($b["size"]) ? -1 : 1;
});
usort($sortingArray,function($a,$b){
//您可以计算您的大小并对结果进行排序
//或者将其拆分为2个数字,然后对其进行排序
返回intval($a[“size”])
阅读您试图实现的目标,我认为以下是实现解决方案的正确方法
由于您需要几种不同的方法根据字段的值对数组进行排序,并根据它们自己的需要,因此我认为一种策略类型的模式将适合此场景
为此,我定义了几个函数,其中一个用于清理包含price的字符串,然后使用spaceship操作符比较它们
对于大小,我们只需获得宽度并比较整数
最后,默认值将只应用常规比较
function priceSort($on)
{
return function ($item, $other) use ($on) {
$itemPrice = filter_var($item[$on], FILTER_SANITIZE_NUMBER_FLOAT);
$otherPrice = filter_var($other[$on], FILTER_SANITIZE_NUMBER_FLOAT);
return floatval($itemPrice) <=> floatval($otherPrice);
};
}
function widthSort($on)
{
return function ($item, $other) use ($on) {
$itemWidth = explode('x', str_replace(' ', '', $item[$on]))[0];
$otherWidth = explode('x', str_replace(' ', '', $other[$on]))[0];
return intval($itemWidth) <=> intval($otherWidth);
};
}
function defaultSort($on)
{
return function ($item, $other) use ($on) {
return $item[$on] <=> $other[$on];
};
}
function determineSortStrategy($on)
{
switch ($on) {
case 'size':
$strategy = widthSort($on);
break;
case 'data-price':
$strategy = priceSort($on);
break;
default:
$strategy = defaultSort($on);
break;
}
return $strategy;
}
function orderBased($strategy, $order)
{
if ($order === SORT_DESC) {
$strategy = function ($item, $other) use ($strategy) {
return -1 * $strategy($item, $other);
};
}
return $strategy;
}
function array_sort($array, $on = 'id', $order = SORT_ASC)
{
$strategy = determineSortStrategy($on);
$strategy = orderBased($strategy, $order);
usort($array, $strategy);
return $array;
}
$result = array_sort($sortingArray, 'data-price', SORT_DESC);
echo '<pre>';
print_r($result);
echo '</pre>';
函数priceSort($on)
{
返回函数($item,$other)使用($on){
$itemPrice=filter\u var($item[$on],filter\u SANITIZE\u NUMBER\u FLOAT);
$otherPrice=filter\u var($other[$on],filter\u SANITIZE\u NUMBER\u FLOAT);
返回floatval($itemPrice)floatval($otherPrice);
};
}
函数宽度排序($on)
{
返回函数($item,$other)使用($on){
$itemWidth=explode('x',str_replace('',$item[$on])[0];
$otherWidth=explode('x',str_replace('',$other[$on])[0];
返回intval($itemWidth)intval($otherWidth);
};
}
函数defaultSort($on)
{
返回函数($item,$other)使用($on){
返回$item[$on]$other[$on];
};
}
函数确定RTStrategy($on)
{
开关($on){
案例“大小”:
$strategy=widthSort($on);
打破
案例“数据价格”:
$strategy=priceSort($on);
打破
违约:
$strategy=defaultSort($on);
打破
}
回报策略;
}
基于订单的函数($strategy,$order)
{
如果($order==排序描述){
$strategy=功能($item,$other)使用($strategy){
返回-1*$策略($项目,$其他);
};
}
回报策略;
}
函数数组\排序($array,$on='id',$order=sort\u ASC)
{
$strategy=determineSortStrategy($on);
$strategy=orderBased($strategy,$order);
usort($阵列,$策略);
返回$array;
}
$result=array\u sort($sortingArray,'data price',sort\u DESC);
回声';
打印(结果);
回声';
建议的解决方案:
函数数组\u排序(数组$array,字符串$on,字符串$order='ASC'){
$sortableArray=$array;
开关($on){
案例“大小”:
usort($sortableArray,'sortBySize');
打破
案例“价格”:
usort($sortableArray,'sortByPrice');
打破
违约:
返回$array;
打破
}
if(strtoupper($order)==“DESC”){
返回数组\u反向($sortableArray);
}
返回$sortableArray;
}
函数sortByPrice(数组$a,数组$b){
返回(浮动)mb_substr($a['data-price'],1)(浮动)mb_substr($b['data-price'],1);
}
函数sortBySize(数组$a、数组$b){
返回值(int)$a['size'](int)$b['size'];
}
工作原理:
- 它切换排序条件,并选择适当的排序回调(从单独定义的函数中),以与
一起使用。如果提供了不支持的值,它将返回原始订单usort
- 数组总是按升序排序(即使某些伪值作为顺序提供)。如果定义了降序,我们将使用返回按相反顺序排序的数组
- 由于您已指定仅使用宽度对大小进行排序,因此通过将大小值强制转换为整数对其进行排序(因为整数值仅考虑第一个非数字字符之前的数字)
- 按价格排序需要提取金额,这是通过
完成的(它消除了第一个字符)。请注意,mb_substr($priceVariable,1)
在这里不起作用,因为磅符号是一个多字节字符,因此必须使用substring函数的多字节版本substr
- 这两种排序功能都使用
最后,请注意,排序条件上的
$与数组键不完全匹配(我使用'price'
而不是'data-price'
;它们可以匹配-就像'size'
一样-但不一定要匹配)。这样做的好处是,如果数组中的索引发生更改,您只需要修改排序回调,而不必在调用自定义数组\u sort
函数的每个地方进行修改。不确定在这里简单地强制转换为int是否有意义<代码>120 x 60
强制转换为int,将导致120。所以每次只考虑第一个维度,第二个维度对排序结果没有任何影响。现在OP并没有具体说明他们想要如何分类,但我的猜测是,一个尺寸10x1
的项目可能会被认为比一个尺寸5x500
的项目小。不简单,但我需要,我试着给你一些想法如何做我的答案,也许类似的事情?谢谢你,从道具开始