I have this array.
$test['color'][0] = "red";
$test['color'][1] = "blue";
$test['color'][2] = "black";
$test['plug'][3] = "US";
$test['plug'][4] = "UK";
我想从上面的数组中实现这个目标。
$test2['color'] = "red,blue,black";
$test2['plug'] = "US,UK";
实现这一目标的最佳逻辑是什么。
答案 0 :(得分:2)
你可以很好地使用一些逻辑和一些PHP函数:
<?php
$array['color'][0] = "red";
$array['color'][1] = "blue";
$array['color'][2] = "black";
$array['plug'][3] = "US";
$array['plug'][4] = "UK";
$test2=array();
foreach($array as $key=>$val)
{
$test2[$key]=implode(',',$val);
}
print_r($test2);
?>
输出:
Array
(
[color] => red,blue,black
[plug] => US,UK
)
编辑:第一个答案是错误的,过于复杂。这是一种控制结构解决方案。
答案 1 :(得分:0)
$test2['color'] = implode(',', $test['color']);
$test2['plug'] = implode(',', $test['plug']);
print_r($test2);