【问题标题】:Move specific array items to beginning of array without altering order of the rest将特定的数组项移动到数组的开头而不改变其余项的顺序
【发布时间】:2015-06-05 10:48:46
【问题描述】:

我有一个数组:

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排序的。

【问题讨论】:

  • 你能发布预期的输出吗
  • 您能否提供更多关于您想要实现的目标的信息?
  • 我已经用我想要实现的目标编辑了这个问题
  • 给定的数组是不可能的(重复键),一个真正的数组样本会有所帮助。
  • 替代方案:通过数组查找最新项的键(foreacharray_reduce),然后将array_splicearray_merge 一起返回。

标签: php arrays sorting


【解决方案1】:

不是最优化的实现,而是最直接的:

$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_splice 操作要求您的数组键实际上是product1 或类似的;不适用于数字索引,因为它们将被重新编号。如果是这种情况,请使用另一种截断机制。

如果你的数组真的很大,那么一个完整的排序会不必要地慢。在这种情况下,您应该循环遍历数组一次,跟踪您可以找到的两个最新项目(及其键),然后跟踪 array_diff_keyarray_merge。这有点难以实现(留给读者作为练习),但效率更高。

【讨论】:

    【解决方案2】:
    // 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);     
    

    【讨论】:

      【解决方案3】:
      // copy current array for new array
      $temp = $input; 
      
      // sort temp array by latest date
      uasort($temp, function($a,$b) {
          return (strtotime($a['extra']['date']) < strtotime($b['extra']['date']));
      });
      
      // for 2 key value pairs to get on top
      $sorted_keys = array_keys($temp);
      
      // initialize your required array
      $final = [];
      
      // two keys to move on top
      $final [ $sorted_keys[0] ] = $temp [ $sorted_keys[0] ];
      $final [ $sorted_keys[1] ] = $temp [ $sorted_keys[1] ];
      
      foreach ($input as $k => $v)
      {
          // insert your other array values except two latest
          if(!array_key_exists($k, $final))
          {
              $final[$k]=$v;
          }
      }
      
      unset($temp); // free up resource
      

      $final 是你需要的数组

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        • 2011-04-09
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        相关资源
        最近更新 更多