Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP使用特定键值对多维数组进行排序_Php_Arrays_Sorting - Fatal编程技术网

PHP使用特定键值对多维数组进行排序

PHP使用特定键值对多维数组进行排序,php,arrays,sorting,Php,Arrays,Sorting,我有一个使用PHP生成的数组,如下所示: Array ( [0] => Array ( [user] => test 1 [esttime] => 5 mins [destination] => testing location svvfefhsrdfd ) [1] => Array ( [user] => test 2 [esttime] => 5 mins [destination] => testing location

我有一个使用PHP生成的数组,如下所示:

Array ( 
    [0] => Array ( [user] => test 1 [esttime] => 5 mins [destination] => testing location svvfefhsrdfd ) 
    [1] => Array ( [user] => test 2 [esttime] => 5 mins [destination] => testing location fsdfdsv  ) 
    [2] => Array ( [user] => test 5 [esttime] => 8 mins [destination] => testing location scvvfe ) 
    [3] => Array ( [user] => test 3 [esttime] => 5 mins [destination] => testing location sfds) 
    [4] => Array ( [user] => test 4 [esttime] => 8 mins [destination] => testing location gfsdarr ) 
    [5] => Array ( [user] => test 6 [esttime] => 10 mins [destination] => testing location dgfd ) 
)
该数组有用户键、估计时间和目标键以及相关值,我需要使用esttime键值对该数组进行排序

我试了很多方法,但找不到一个方法


任何人都知道如何使用php对该数组进行排序,谢谢

一种方法是生成一个新数组:

foreach($array as $row) {
        $new[str_replace(" ","_",$row['esttime'])][] = $row;
    }
print\r($new)后面应该是这样的:

Array (
        [5_mins][0] => Array ([user] => test 1 [esttime] => 5 mins [destination] => testing location svvfefhsrdfd)
        [5_mins][1] => Array ( [user] => test 2 [esttime] => 5 mins [destination] => testing location fsdfdsv)
        [5_mins][2] => Array ( [user] => test 3 [esttime] => 5 mins [destination] => testing location sfds)
        [8_mins][0] => Array ( [user] => test 5 [esttime] => 8 mins [destination] => testing location scvvfe )
        [8_mins][1] => Array ( [user] => test 4 [esttime] => 8 mins [destination] => testing location gfsdarr )
        [10_mins][0] => Array ( [user] => test 6 [esttime] => 10 mins [destination] => testing location dgfd )
      )

在这种情况下,您可以使用自定义排序
usort()
,然后只需对该相对时间使用
strotime()

usort($array, function($a, $b){
    $time_a = strtotime($a['esttime']);
    $time_b = strtotime($b['esttime']);

    return $time_a - $time_b;
});

echo '<pre>';
print_r($array);
usort($array,function($a,$b){
$time_a=strottime($a['esttime']);
$time_b=strottime($b['esttime']);
返回$time\u a-$time\u b;
});
回声';
打印(数组);

我喜欢这个主意,这是一个明智的做法。我从来没有想到过一个
strotime()
…很明显,我的回答是…@Rasclatt这是一个标准的方法,这里没有什么特别的。@Rasclatt我也许能更好地处理时间,由于我们不知道OP可能会有一个模糊的情况,即
5分钟3小时
等。在它的时间范围内处理它可能会更好。好吧,我还是学到了一些东西(即使它对其他人来说没什么特别的)。@smithbandara很高兴这有助于
usort
O(nlogn)
上工作,而这种方式在
O(n)
上工作。更快的算法。@这就像分组,而不是排序。OP的样本数据只是一个例子,我不认为这可以对更复杂的东西进行排序。例如,尝试使用此数据
6分钟、5分钟、10分钟
(请注意,OP的数据几乎已排序,因此这适用于该数据)。