如何转换多维数组,如下面的
Array(
[0] => Array(
[0]=001
[1]=002
[2]=003
)
[1] => Array(
[0]=America
[1]=Japan
[2]=South Korea
)
[2] => Array(
[0]=Washington DC
[1]=Tokyo
[2]=Seoul
)
)
进入一个如下所示的单行数组?
Array(
[0]=001,America,Washington DC
[1]=002,Japan,Tokyo
[2]=003,South Korea,Seoul
)
答案 0 :(得分:1)
这是一个简单易用的代码,
foreach ($text as $key => $value) {
foreach ($value as $key1 => $value1) {
$result[$key1][] = $value1;
}
}
array_walk($result, function(&$item){
$item = implode(',', $item);
});
这是工作link
array_walk - 将用户提供的函数应用于数组的每个成员
答案 1 :(得分:0)
variadiac php5.6 +版本:(提供额外的好处,即不打破缺失值并插入缺少值的null
。)
代码:(Demo)
var_export(array_map(function(){return implode(',',func_get_args());},...$text));
非可变版本:
代码:(Demo)
foreach($text as $i=>$v){
$result[]=implode(',',array_column($text,$i));
}
var_export($result);
输入:
$text = [
['001','002','003'],
['America','Japan','South Korea'],
['Washington DC','Tokyo','Seoul']
];
两种方法的输出:
array (
0 => '001,America,Washington DC',
1 => '002,Japan,Tokyo',
2 => '003,South Korea,Seoul',
)
几乎完全相同的重复页面:Combining array inside multidimensional array with same key