gpt4 book ai didi

PHP 使用空值对位置进行排序但保持结构

转载 作者:行者123 更新时间:2023-12-04 10:30:08 25 4
gpt4 key购买 nike

我想使用数据库中可能存在或不存在的值对数组进行排序,并且应该遵守顺序结构。

默认结构(位置从 1 到 5):

Amazon | Google | Ebay | Microsoft | Alibaba

这个结构在 PHP 中是这样初始化的:
$data = 
[
'Amazon' => ['position' => null],
'Google' => ['position' => null],
'Ebay' => ['position' => null],
'Microsoft' => ['position' => null],
'Alibaba' => ['position' => null]
];

重要 : 存储在数据库中的位置总是等于或大于 1。

假设谷歌在数据库中的位置为 1,阿里巴巴为 4:
$data['Google']['position'] = $fromDb->google->position; // 1
$data['Alibaba']['position'] = $fromDb->alibaba->position; // 4

如果我使用 array_multisort 对数组进行排序功能如下:
$sort = [];

foreach ($data as $key => $value)
$sort[$key] = $value['position'];

array_multisort($sort, SORT_ASC, $data);

输出 :
Array
(
[Amazon] =>
[Ebay] =>
[Microsoft] =>
[Google] => 1
[Alibaba] => 4
)

所需输出 :
Array
(
[Google] => 1
[Amazon] => 2
[Ebay] => 3
[Alibaba] => 4
[Microsoft] => 5
)

最佳答案

在做一个简单的之前填写缺失值 usort :

$data = [
'Amazon' => ['position' => null],
'Google' => ['position' => 1],
'Ebay' => ['position' => null],
'Microsoft' => ['position' => null],
'Alibaba' => ['position' => 4]
];

// Find existing positions.
$positions = array_filter(array_column($data, 'position'));
$i = 1;

foreach ($data as &$comp) {
if ($comp['position']) {
// Element already has a position, skip to next one.
continue;
}
while (in_array($i, $positions)) {
// Increment the counter until we find a value not yet taken.
$i++;
}
// Assign the counter value to the current element.
$comp['position'] = $i++;
}
unset($comp);

// Sort all values with a simple comparison function.
uasort($data, function ($a, $b) { return $a['position'] <=> $b['position']; });

有点发烧友:
// Compute the *missing* positions by subtracting the existing positions
// (extracted via array_column) from the set of possible positions
// (generated with range()).
$positions = array_diff(range(1, count($data)), array_filter(array_column($data, 'position')));

// Apply the missing positions to the array elements in order
// (taking positions off the beginning with array_shift).
array_walk($data, function (&$i) use (&$positions) {
if (!$i['position']) {
$i['position'] = array_shift($positions);
}
});

// Sort all values with a simple comparison function.
uasort($data, function ($a, $b) { return $a['position'] <=> $b['position']; });

关于PHP 使用空值对位置进行排序但保持结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60454029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com