将二维数组排序为降序列值的重复序列

问题描述 投票:0回答:1

我试图根据数组优先级从最高到最低对数组(turns_request)进行排序,每个优先级仅获取一个数据,如果其中任何一个重复,则必须完成顺序才能重新开始; [9,9,7,2,2,2,2] 排序后将是 [9,7,2,9,2,2,2]。

这是我的代码:

$turns_request = Turn::where('turns.status', 'pending')
            ->whereBetween('turns.created_at', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])
            ->where('turns.branch_id', Auth::user()->branch_id)
            ->join('turn_types as tt', 'turns.turn_type_id', '=', 'tt.id')
            ->orderBy('tt.priority', 'desc')
            ->orderBy('turns.id', 'asc')
            ->select('turns.id', 'turns.turn', 'turns.is_scheduled', 'turns.licence_type_id', 'turns.turn_type_id', 'turns.module_id', 'turns.branch_id', 'turns.status', 'turns.attending_start', 'turns.attending_end', 'turns.appointment_folio', 'turns.created_at', 'turns.updated_at', 'tt.priority', 'tt.turn_qty')
            ->get()
            ->toArray(); 
        
        $priorities = TurnType::where('branch_id', Auth::user()->branch_id)->orderBy('priority', 'desc')->get()->toArray();
        $turns = array();
        $turns_number = count($turns_request);


        while (count($turns) < $turns_number) {
            foreach ($priorities as $priority) { // [9,7,6,3,2,0]
                if(count($turns) < count($turns_request)){
                    $turnKey = array_search($priority['priority'], array_column($turns_request, 'priority'));
                    if ($turnKey !== false) {
                        $turns[] = $turns_request[$turnKey];
                        unset($turns_request[$turnKey]);
                    }
                }else{
                    break;
                }
            }
        }

我进行了调试,发现在前两次迭代中,所有内容都正确打印,但在第三次迭代中,array_search 返回数组元素 1 作为索引,但这在之前的迭代中已被删除。我不知道为什么会这样,希望您能帮助我,谢谢!

[2024-01-08 22:25:05] local.DEBUG: turnKey: 0 preference: 9   
[2024-01-08 22:25:05] local.DEBUG: Array after deletion  
[2024-01-08 22:25:05] local.DEBUG: array (
  1 => 
  array (
    'id' => 30,
    'turn' => 'PD0007',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 6,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:54:11',
    'updated_at' => '2024-01-03 23:54:11',
    'priority' => 9,
    'turn_qty' => 1,
  ),
  2 => 
  array (
    'id' => 27,
    'turn' => 'PV0005',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 5,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:53:15',
    'updated_at' => '2024-01-03 23:53:15',
    'priority' => 8,
    'turn_qty' => 1,
  ),
  3 => 
  array (
    'id' => 1,
    'turn' => 'SC0001',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 4,
    'module_id' => 1,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 21:51:16',
    'updated_at' => '2024-01-02 22:06:58',
    'priority' => 7,
    'turn_qty' => 1,
  ),
  4 => 
  array (
    'id' => 25,
    'turn' => 'TC0006',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 2,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:53:05',
    'updated_at' => '2024-01-03 23:53:05',
    'priority' => 6,
    'turn_qty' => 1,
  ),
  5 => 
  array (
    'id' => 29,
    'turn' => 'TC0007',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 2,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:54:05',
    'updated_at' => '2024-01-03 23:54:05',
    'priority' => 6,
    'turn_qty' => 1,
  ),
  6 => 
  array (
    'id' => 22,
    'turn' => 'MA0006',
    'is_scheduled' => 0,
    'licence_type_id' => 2,
    'turn_type_id' => 1,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 21:57:13',
    'updated_at' => '2024-01-03 21:57:13',
    'priority' => 5,
    'turn_qty' => 1,
  ),
)  
[2024-01-08 22:25:05] local.DEBUG: turnKey: 1 preference: 8   
[2024-01-08 22:25:05] local.DEBUG: Array after deletion   
[2024-01-08 22:25:05] local.DEBUG: array (
  2 => 
  array (
    'id' => 27,
    'turn' => 'PV0005',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 5,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:53:15',
    'updated_at' => '2024-01-03 23:53:15',
    'priority' => 8,
    'turn_qty' => 1,
  ),
  3 => 
  array (
    'id' => 1,
    'turn' => 'SC0001',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 4,
    'module_id' => 1,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 21:51:16',
    'updated_at' => '2024-01-02 22:06:58',
    'priority' => 7,
    'turn_qty' => 1,
  ),
  4 => 
  array (
    'id' => 25,
    'turn' => 'TC0006',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 2,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:53:05',
    'updated_at' => '2024-01-03 23:53:05',
    'priority' => 6,
    'turn_qty' => 1,
  ),
  5 => 
  array (
    'id' => 29,
    'turn' => 'TC0007',
    'is_scheduled' => 0,
    'licence_type_id' => 1,
    'turn_type_id' => 2,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 23:54:05',
    'updated_at' => '2024-01-03 23:54:05',
    'priority' => 6,
    'turn_qty' => 1,
  ),
  6 => 
  array (
    'id' => 22,
    'turn' => 'MA0006',
    'is_scheduled' => 0,
    'licence_type_id' => 2,
    'turn_type_id' => 1,
    'module_id' => NULL,
    'branch_id' => 1,
    'status' => 'pending',
    'attending_start' => NULL,
    'attending_end' => NULL,
    'appointment_folio' => NULL,
    'created_at' => '2024-01-08 21:57:13',
    'updated_at' => '2024-01-03 21:57:13',
    'priority' => 5,
    'turn_qty' => 1,
  ),
)  
[2024-01-08 22:25:05] local.DEBUG: turnKey: 1 preference: 7  
[2024-01-08 22:25:05] local.ERROR: http://127.0.0.1:8000/turns-capturer-data  
[2024-01-08 22:25:05] local.ERROR: Undefined array key 1 in C:\CST\app\Http\Controllers\TurnsController.php in line 245  
php laravel sorting recurring custom-sort
1个回答
0
投票

如果我有误解,请纠正我,但是

$priorities
数组的总体是无用的,因为您正在构建一个在迭代其他结果集时预计会遇到的值列表,并且您希望它们按降序排列。好吧,你不需要这个......只需按优先级递减的重复序列进行排序即可。

下面是我的答案的调整/简化实现:按列值将 2D 数组排序为不超过 N 种的重复升序组

本质上,您按三个长度相等的数组进行排序:

  • 按第n次遇到值ASC排序,然后
  • 按实际列值DESC排序,然后
  • 最终的参数不会有任何关系需要打破,但它将是修改后的结果。

代码:(演示

$turns_request = [
    ['id' => 22, 'priority' => 5],
    ['id' => 23, 'priority' => 5],
    ['id' => 9, 'priority' => 6],
    ['id' => 19, 'priority' => 6],
    ['id' => 25, 'priority' => 6],
    ['id' => 29, 'priority' => 6],
    ['id' => 1, 'priority' => 7],
    ['id' => 21, 'priority' => 7],
    ['id' => 14, 'priority' => 8],
    ['id' => 27, 'priority' => 8],
    ['id' => 3, 'priority' => 9],
    ['id' => 30, 'priority' => 9],
];

$grouper = [];
$column = [];
foreach ($turns_request as ['priority' => $p]) {
    $column[] = $p;
    $encountered[$p] ??= 0;
    $grouper[] = $encountered[$p]++;
}

array_multisort($grouper, $column, SORT_DESC, $turns_request);

var_export($turns_request);

输出(手动压缩格式以提高可读性):

[
    ['id' => 3, 'priority' => 9],
    ['id' => 14, 'priority' => 8],
    ['id' => 1, 'priority' => 7],
    ['id' => 9, 'priority' => 6],
    ['id' => 22, 'priority' => 5,
    ['id' => 30, 'priority' => 9],
    ['id' => 27, 'priority' => 8],
    ['id' => 21, 'priority' => 7],
    ['id' => 19, 'priority' => 6],
    ['id' => 23, 'priority' => 5],
    ['id' => 25, 'priority' => 6],
    ['id' => 29, 'priority' => 6],
]
© www.soinside.com 2019 - 2024. All rights reserved.