dsms21398 2018-10-17 11:33
浏览 8
已采纳

OUTPUT单独从数组中键入1个键

How to make if _from is Alone to _to --- to Select _Alone with the higher ID

Drop else from array

array (
  0 => 
  array (
    'id' => '8',
    '_from' => '2',
    '_to' => '1',
    'date' => '2018-10-15 15:51:07',
    'message' => 'ccccccxxxxx',
    'read' => '0',
    'feedback' => '0',
    'cnt' => '3',
  ),
  1 => 
  array (
    'id' => '6',
    '_from' => '1',
    '_to' => '2',
    'date' => '2018-10-15 15:47:01',
    'message' => 'zzzzzzz1',
    'read' => '1',
    'feedback' => '0',
    'cnt' => '1',
  ),
  • 写回答

1条回答 默认 最新

  • donglan7594 2018-10-19 11:30
    关注

    If you have small arrays, It is not a problem to run the algorithm with complexity O(n2). But for me better is less clear, but faster algorithm with complexity equals to O(2n)

    $array = array(
        array(
            'id'         => 12,
            '_from'      => 1,
            '_to'        => 2
        ),
        array(
            'id'         => 13,
            '_from'      => 4,
            '_to'        => 2
        ),
        array(
            'id'         => 14,
            '_from'      => 2,
            '_to'        => 1
        ),
    );
    $newArray = [];
    foreach ($array as $item) {
        $uniqueRecordKey = $item['_from'].'-'.$item['_to'];
        $oppositeRecordKey = $item['_to'].'-'.$item['_from'];
    
        //If exists record from the opposite and new ID is greater than previous put
        if (isset($newArray[$oppositeRecordKey])) {
            $newArray[$oppositeRecordKey] = $item;
            continue; //Do not append to the end
        }
    
        $newArray[$uniqueRecordKey] = $item;
    }
    
    var_dump($newArray);
    

    https://3v4l.org/0oL2d

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条