使用 PHP 按 id 将数组拆分为等份顺序
Split array into equal parts order by id using PHP
我有一个如下所示的数组列表,如何将它们分成相等的部分,按 id 对相等的部分进行排序,并使用 PHP 在 id 中合并相等的部分?
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
[2] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[3] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
[4] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
假设我想把它们分成 2 个相等的项目,
先拆分,最小的id先平分,应该是这样的:
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
)
Array
(
[0] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[1] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
)
Array
(
[0] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
我的示例代码是5项,如果是6项,应该是[1,2],[3,4],[5,6],多少项或部分不确定,但我应该是 2 个相等的部分。
其次合并(这是我们项目的逻辑,如果你不懂我在说什么,请忽略合并问题,我只需要知道如何拆分):
[121, 139] => 合并到 139
,
[1456, 1896] => 合并到 1896
,
新列表:[139, 1896] => 合并到 1896
,
最后的列表 [1896, 1963] 合并到最终的 id 1963
听起来你想要这样的东西:
$array = [
[
'id' => 121,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 139,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 1456,
'owner' => 'xv',
'name' => 'bjs',
],
[
'id' => 1896,
'owner' => 'xb',
'name' => 'bjs',
],
[
'id' => 1963,
'owner' => 'xb',
'name' => 'bjs',
]
];
// custom function to compare which ID is greater
function customCompare($a, $b)
{
if ($a['id'] === $b['id']) {
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
// sort the whole array
usort($array, "customCompare");
// print the whole array as pairs,
// if there is an unpair number
// the last one will be a single member
print_r(array_chunk($array, 2));
我有一个如下所示的数组列表,如何将它们分成相等的部分,按 id 对相等的部分进行排序,并使用 PHP 在 id 中合并相等的部分?
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
[2] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[3] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
[4] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
假设我想把它们分成 2 个相等的项目, 先拆分,最小的id先平分,应该是这样的:
Array
(
[0] => Array
(
[id] => 121
[owner] => xa
[name] => xjs
)
[1] => Array
(
[id] => 139
[owner] => xa
[name] => xjs
)
)
Array
(
[0] => Array
(
[id] => 1456
[owner] => xv
[name] => bjs
)
[1] => Array
(
[id] => 1896
[owner] => xb
[name] => bjs
)
)
Array
(
[0] => Array
(
[id] => 1963
[owner] => xb
[name] => bjs
)
)
我的示例代码是5项,如果是6项,应该是[1,2],[3,4],[5,6],多少项或部分不确定,但我应该是 2 个相等的部分。
其次合并(这是我们项目的逻辑,如果你不懂我在说什么,请忽略合并问题,我只需要知道如何拆分):
[121, 139] => 合并到 139
,
[1456, 1896] => 合并到 1896
,
新列表:[139, 1896] => 合并到 1896
,
最后的列表 [1896, 1963] 合并到最终的 id 1963
听起来你想要这样的东西:
$array = [
[
'id' => 121,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 139,
'owner' => 'xa',
'name' => 'xjs',
],
[
'id' => 1456,
'owner' => 'xv',
'name' => 'bjs',
],
[
'id' => 1896,
'owner' => 'xb',
'name' => 'bjs',
],
[
'id' => 1963,
'owner' => 'xb',
'name' => 'bjs',
]
];
// custom function to compare which ID is greater
function customCompare($a, $b)
{
if ($a['id'] === $b['id']) {
return 0;
}
return ($a['id'] < $b['id']) ? -1 : 1;
}
// sort the whole array
usort($array, "customCompare");
// print the whole array as pairs,
// if there is an unpair number
// the last one will be a single member
print_r(array_chunk($array, 2));