首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP排序数组2值

PHP排序数组2值
EN

Stack Overflow用户
提问于 2017-10-08 10:37:40
回答 1查看 20关注 0票数 0

我要把“时间”和“支票”分类。

对于第一次排序(时间),它是有效的。

但是第二次排序(校验),它不起作用。

我需要更高的价格。

50-3-3-3

(77-3).=‘3’>

46-3

84-2

74-2

74-2

第48-2条-第48-2条

75-1

70-1

46-1

请帮帮忙。

代码语言:javascript
运行
复制
<?php

$dbx['x1']['inplay']['time'] = 84;
$dbx['x2']['inplay']['time'] = 77;
$dbx['x3']['inplay']['time'] = 75;
$dbx['x4']['inplay']['time'] = 74;
$dbx['x5']['inplay']['time'] = 74;
$dbx['x6']['inplay']['time'] = 70;
$dbx['x7']['inplay']['time'] = 50;
$dbx['x8']['inplay']['time'] = 48;
$dbx['x9']['inplay']['time'] = 46;
$dbx['x10']['inplay']['time'] = 46;

$dbx['x1']['inplay']['checkdanum'] = 2;
$dbx['x2']['inplay']['checkdanum'] = 3;
$dbx['x3']['inplay']['checkdanum'] = 1;
$dbx['x4']['inplay']['checkdanum'] = 2;
$dbx['x5']['inplay']['checkdanum'] = 2;
$dbx['x6']['inplay']['checkdanum'] = 1;
$dbx['x7']['inplay']['checkdanum'] = 3;
$dbx['x8']['inplay']['checkdanum'] = 2;
$dbx['x9']['inplay']['checkdanum'] = 3;
$dbx['x10']['inplay']['checkdanum'] = 1;



$dbx = desc_sort($dbx , 'time');

$dbx = desc_sort($dbx,'checkdanum');
echo '<pre>';
foreach($dbx as $k => $v){
echo $dbx[$k]['inplay']['time'].'-'.$dbx[$k]['inplay']['checkdanum'].'<br>';
}

function desc_sort ($array, $key) {
    $sorter = array();
    $ret = array();
    reset($array);
    foreach ($array as $ii => $va) {
        foreach ($array[$ii] as $k => $v){
            if($k == 'inplay'){
            $sorter[$ii] = $v[$key];
            }
        }
    }

    if (!function_exists('myComparison')) {
        function myComparison($a, $b){
            if(is_numeric($a) && !is_numeric($b))
            return -1;
            else if(!is_numeric($a) && is_numeric($b))
            return 1;
            else
            return ($a < $b) ? 1 : -1;
        }
    }

    uasort ( $sorter , 'myComparison' );
    foreach ($sorter as $ii => $va) {
        $ret[$ii] = $array[$ii];
    }
    return $ret;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-08 10:46:35

你可以同时对两者进行排序。如果您使用的是PHP,您可以通过7+保存一些比较。例如:

代码语言:javascript
运行
复制
<?php   
uasort($dbx, function ($x,$y) {
   if (($x["inplay"]["checkdanum"] <=> $y["inplay"]["checkdanum"]) === 0) {
       return -($x["inplay"]["time"] <=> $y["inplay"]["time"]);
   } 
   return -($x["inplay"]["checkdanum"] <=> $y["inplay"]["checkdanum"]);
});

这个似乎很管用非常简单。

对于较早的PHP版本,您可以返回定义为"myComparison“的函数。

注意:您可以在检查索引是否也存在的情况下对其进行填充。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46630127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档