如何在laravel中按唯一性对数组排序[duplicate]

pb3skfrl  于 2023-04-13  发布在  其他
关注(0)|答案(3)|浏览(153)

此问题已在此处有答案

Sort a flat array in recurring ascending sequences(5个答案)
2天前关闭。
我有一个带值的数组列表 在每个数组中。我想根据这些值中的一个对列表进行排序 这样唯一的数组会先显示,然后是重复的数组。我不想从数组中删除任何成员,我只想对数组进行排序
这是我的名单

array:6 [▼
  0 => array:8 [▼
    "id" => 35
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  1 => array:8 [▼
    "id" => 36
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  2 => array:8 [▼
    "id" => 37
    "answer" => "Gello"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  3 => array:8 [▼
    "id" => 38
    "answer" => "Uho"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  4 => array:8 [▼
    "id" => 39
    "answer" => "Who"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  5 => array:8 [▼
    "id" => 40
    "answer" => "Window"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:40.000000Z"
    "updated_at" => "2023-04-07T09:03:40.000000Z"
  ]
]

我想按question_id排序。我希望我的列表最终变成这个列表

array:6 [▼
  0 => array:8 [▼
    "id" => 35
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  2 => array:8 [▼
    "id" => 37
    "answer" => "Hello"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  4 => array:8 [▼
    "id" => 39
    "answer" => "Who"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  1 => array:8 [▼
    "id" => 36
    "answer" => null
    "type" => 3
    "user_id" => 9
    "question_id" => 36
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]
  3 => array:8 [▼
    "id" => 38
    "answer" => "Uho"
    "type" => 0
    "user_id" => 9
    "question_id" => 37
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:39.000000Z"
    "updated_at" => "2023-04-07T09:03:39.000000Z"
  ]

  5 => array:8 [▼
    "id" => 40
    "answer" => "Window"
    "type" => 1
    "user_id" => 9
    "question_id" => 38
    "answer_id" => 1
    "created_at" => "2023-04-07T09:03:40.000000Z"
    "updated_at" => "2023-04-07T09:03:40.000000Z"
  ]
]
p1tboqfb

p1tboqfb1#

实际上,您需要使用map/set来存储每个问题ID的行,然后使用array_columnarray_merge使它们按组显示。

**注意:**由于您的是Laravel集合,您可以简单地使用->toArray()将其转换为PHP数组,并使用以下代码!
片段:

<?php

$set = [];
$result = [];
$maxColumns = 0;

foreach($records as $r){
   $set[ $r['question_id'] ] = $set[ $r['question_id'] ] ?? [];
   $set[ $r['question_id'] ][] = $r;
   $maxColumns = max($maxColumns, count($set[ $r['question_id'] ]));
}

for($i = 0; $i < $maxColumns; ++$i){
    $result = array_merge($result, array_column($set, $i));
}

print_r($result);

Online Demo

vs3odd8k

vs3odd8k2#

好吧,它看起来像是从DB返回的数据,这取决于你使用的是什么:

  • DB class -〉https://laravel.com/docs/10.x/queries#orderingDB::table('table_name')->orderBy('question_id')->get()->toArray();
  • Eloquent -〉https://laravel.com/docs/10.x/eloquent#retrieving-modelsModel::orderBy('question_id)->get()->toArray();
hgqdbh6s

hgqdbh6s3#

正如你提到的,你有一个数组,这可能是一个合适的解决方案

<?php 
$records =[
  [
    "id" => 1,
    "user_id" => 9,
    "question_id" => 16,
    "answer_id" => 1,
  ],
  [
    "id" => 2,
    "user_id" => 9,
    "question_id" => 26,
    "answer_id" => 1,
  ],
  [
    "id" => 3,
    "user_id" => 9,
    "question_id" => 6,
    "answer_id" => 1,
  ],
  [
    "id" => 13,
    "user_id" => 9,
    "question_id" => 36,
    "answer_id" => 1,
  ],
  [
    "id" => 18,
    "user_id" => 9,
    "question_id" => 26,
    "answer_id" => 1,
  ],
    [
    "id" => 21,
    "user_id" => 9,
    "question_id" => 06,
    "answer_id" => 1,
  ],
  [
    "id" => 21,
    "user_id" => 9,
    "question_id" => 11,
    "answer_id" => 1,
  ],
];

function sortCallback($firstRecord,$secondRecord){
    return $firstRecord['question_id']>$secondRecord['question_id'] ;
}
$recordsCopy=$records;
usort($recordsCopy,"sortCallback");
print_r($recordsCopy);

?>

但是它是一个Larvel集合,那么你可以用相同的回调方法对它进行排序。

相关问题