如果“u”、“v”和“w”键下的值相同,我想合并多维数组值。
$myarray = array (
'0' =>
array (
'u' => '30',
'v' => '16',
'w' => '22',
'x' => '30',
'y' => '16',
'z' => '22',
),
'1' =>
array (
'u' => '32',
'v' => '25',
'w' => '1',
'x' => '30',
'y' => '16',
'z' => '22',
),
'2' =>
array (
'u' => '30',
'v' => '16',
'w' => '22',
'x' => '54',
'y' => '96',
'z' => '2',
),
'3' =>
array (
'u' => '30',
'v' => '16',
'w' => '22',
'x' => '3',
'y' => '1',
'z' => '6',
)
);
我希望输出如下:
//OUTPUT
array (
'0' =>
array (
'u' => '30',
'v' => '16',
'w' => '22',
'x' => '30,54,3',
'y' => '16,96,1',
'z' => '22,2,6',
),
'1' =>
array (
'u' => '32',
'v' => '25',
'w' => '1',
'x' => '30',
'y' => '16',
'z' => '22',
)
)
这是我迄今为止尝试做的事情,但对我来说还没有成功。
<?php
$n = 0;
$output = [];
foreach ($myarray as $row) {
$prevkey = key(array_slice($output, -1,1,true));// get previous array key
if(array_key_exists('0', $output)){
if($output[$prevkey]['u'] == $row['u'] && $output[$prevkey]['v'] == $row['v'] && $output[$prevkey]['w'] == $row['w'] ){
echo "yes <br>";
$output += $row;
}
}else{
$output[] = $row;
}
$n++;
}
var_dump($output);
?>
您可以使用
array_reduce
:
$grouped = array_reduce($myarray, function ($result, $item) {
$key = '';
foreach (['u', 'v', 'w'] as $prop) {
$key .= $item[$prop] . '_';
}
if (array_key_exists($key, $result)) {
foreach (['x', 'y', 'z'] as $prop) {
$result[$key][$prop] .= ',' . $item[$prop];
}
} else {
$result[$key] = $item;
}
return $result;
}, []);
$result = array_values($grouped);
您可以根据这 3 个值创建一些具有特定索引的特定数组。例如:
$myarray = [...];
$supp = [];
foreach ($myarray as $ind => $arr){
// remember 3 values and use them as a new index
$uvw = sprintf('%d|%d|%d', $arr['u'], $arr['v'], $arr['w']);
if (!isset($supp[$uvw])){
$supp[$uvw] = ['ind' => $ind, 'data' => $arr];
} else {
foreach($arr as $in => $new_val){
foreach($supp[$uvw]['data'] as $i => &$val){
if ($i === $in && !in_array($i,['u','v','w'])){
$val .= ',' . $new_val;
}
}
}
}
}
// recombine the output data (it also includes the parent index value)
$output = [];
foreach($supp as $arr_data){
$output[$arr_data['ind']] = $arr_data['data'];
}
print_r($output);
输出:
Array
(
[0] => Array
(
[u] => 30
[v] => 16
[w] => 22
[x] => 30,54,3
[y] => 16,96,1
[z] => 22,2,6
)
[1] => Array
(
[u] => 32
[v] => 25
[w] => 1
[x] => 30
[y] => 16
[z] => 22
)
)
如果将引用变量推入结果数组,则可以在单个循环中完成这种按多列的分组,而无需在最后重新索引。
vsprintf()
是一种从每行中的前 3 个值创建分隔字符串的简单方法 - 如果您需要显式命名标识键,因为它们不是可靠地位于行中的第一个,那么您可以手动访问它们来构建复合键。
仅当多次遇到一个组时才执行串联。
代码:(演示)
$result = [];
foreach ($array as $row) {
$key = vsprintf('%d_%d_%d', $row);
if (!isset($ref[$key])) {
$ref[$key] = $row;
$result[] =& $ref[$key];
} else {
$ref[$key]['x'] .= ',' . $row['x'];
$ref[$key]['y'] .= ',' . $row['y'];
$ref[$key]['z'] .= ',' . $row['z'];
}
}
var_export($result);