我本来有这个数组
Array
(
[0] => Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25
[1] => Logico deduttive^7^^21,^0.75^0^-0.25
[2] => Situazionali^8^^20,^0.75^0^0.375
)
使用函数explosion和array_diff我可以得到这个
Array
(
[0] => Amministrativo
[1] => 25
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,
[4] => 0.75
[5] => 0
[6] => 0.25
)
Array
(
[0] => Logico deduttive
[1] => 7
[2] =>
[3] => 21,
[4] => 0.75
[5] => 0
[6] => -0.25
)
Array
(
[0] => Situazionali
[1] => 8
[2] =>
[3] => 20,
[4] => 0.75
[5] => 0
[6] => 0.375
)
,但我想连接每个数组的元素,以获得一个唯一的数组。我想我需要使用array_map函数,但我不知道怎么用。下面是我想要达到的结果
Array (
[0] => Amministrativo Logico deduttive Situazionali
[1] => 25 7 8
[2] => 9,11,2,10,18,4,7,
[3] => 17,13,21,20,
[4] => 0.75 0.75 0.75
[5] => 0 0 0
[6] => 0.25 -0.25 0.375
)
秋明石油公司
编辑:我尝试了这里的代码,它很好。但现在我意识到还有一个问题数组可以是变量1 2 3或更多我之前不知道,我应该调整这个代码
$result = array_map(function ($item1, $item2,$item3) {
return "$item1 $item2 $item3";
}, $test[0], $test[1],$test[2]);
原始数组的行被explosion分隔。结果是一个二维数组。这是转置(交换行和列)。然后用内爆重新组装线。
function array_transpose(array $a) {
$r = array();
foreach($a as $keyRow => $subArr) {
foreach($subArr as $keyCol => $value) $r[$keyCol][$keyRow] = $value;
}
return $r;
}
$arr = [
'Amministrativo ^25^9,11,2,10,18,4,7,^17,13,^0.75^0^0.25',
'Logico deduttive^7^^21,^0.75^0^-0.25',
'Situazionali^8^^20,^0.75^0^0.375',
];
foreach($arr as $i => $row){
$arr[$i] = explode('^',$row);
}
$arr = array_transpose($arr);
foreach($arr as $i => $row){
$arr[$i] = implode(' ',$row);
}
var_export($arr);
演示:https://3v4l.org/IdsDh