Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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

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,我有一个数组: Array ( [product1] => Array ( [id] => 1 [title] => 'p1' [extra] => Array( [date] => '1990-02-04 16:40:26' ) ) [product2] => Array ( [id] => 2

我有一个数组:

Array
(
    [product1] => Array
    (
        [id] => 1
        [title] => 'p1'
        [extra] => Array(
            [date] => '1990-02-04 16:40:26'
        )
    )

    [product2] => Array
    (
        [id] => 2
        [title] => 'p2'
        [extra] => Array(
            [date] => '1980-01-04 16:40:26'
        )
    )
    [product3] => Array
    (
        [id] => 3
        [title] => 'p3'
        [extra] => Array(
            [date] => '2000-01-04 16:40:26'
        )
    )
    [product4] => Array
    (
        [id] => 4
        [title] => 'p4'
        [extra] => Array(
            [date] => '1995-01-04 16:40:26'
        )
    )
    [product5] => Array
    (
        [id] => 5
        [title] => 'p5'
        [extra] => Array(
            [date] => '1960-01-04 16:40:26'
        )
    )
    ...
我需要获得2个最新日期的产品,并将它们移动到阵列的开头

我已经研究了multisort函数,我可以像这样对数组进行排序,但是整个数组将按日期进行排列,我想保持数组的顺序,但只是增加最近的2行

我需要从数组中选择2个最新的(按日期排序),然后将它们移动到数组的开头。因此,ID的顺序应为:

3,4,1,2,5
最新的2个已移到数组的前面,其余的仍按id排序。

//为新数组复制当前数组
// Making array with only dates
$dates = array();
foreach ($arr as $key => $item) 
    $dates[$key] = $item['extra']['date'];
// Sort it by date saving keys
uasort($dates, function($i1, $i2) { return  strtotime($i1) - strtotime($i2); });
// Take keys
$dates = array_keys($dates);
// Create array with two needed items
$newarray = array( $dates[0] => $arr[$dates[0]], $dates[1] => $arr[$dates[1]]);
// remove these items
unset($arr[$dates[0]]);  unset($arr[$dates[1]]); 
// put them in array start
$arr = array_merge($newarray, $arr);
var_dump($arr);     
$temp=$input; //按最新日期对临时数组排序 uasort($temp,function($a,$b){ 返回(strotime($a['extra']['date'])v的输入) { //插入除两个最新数组值之外的其他数组值 如果(!array_key_存在($k,$final)) { $final[$k]=$v; } } 未设置($temp);//释放资源

$final
是您所需的阵列

不是最理想的实现,而是最直接的:

$array = /* your data */;

$latest = $array;
uasort($latest, function (array $a, array $b) {
    return strtotime($a['extra']['date']) - strtotime($b['extra']['date']);
});
array_splice($latest, 2);

$latestOnTop = array_merge($latest, array_diff_key($array, $latest));
array\u-splice
操作要求您的数组键实际上是
product1
或类似产品;无法使用数字索引,因为它们将被重新编号。如果是这种情况,请使用另一种截断机制


如果您的数组非常大,则完整的排序速度将非常慢。在这种情况下,您应该在数组上循环一次,跟踪您可以找到的两个最新项目(及其键),然后在该项目上执行
array\u diff\u key
array\u merge
。实现起来有点困难(留给读者做练习),但效率要高得多。

你能发布预期的输出吗?你能提供更多关于你想要实现什么的信息吗?我已经用我想要实现的内容编辑了这个问题给定的数组是不可能的(重复键),一个真实的数组样本会有所帮助。另一种选择是:通过数组查找最新项目的键(
foreach
array\u reduce
),然后将
array\u拼接
array\u合并
返回到一起。
$array = /* your data */;

$latest = $array;
uasort($latest, function (array $a, array $b) {
    return strtotime($a['extra']['date']) - strtotime($b['extra']['date']);
});
array_splice($latest, 2);

$latestOnTop = array_merge($latest, array_diff_key($array, $latest));