break array as a string
I have a array:-
Array ( [6] => 1 [6(HL)] => 3 [5] => 1 [7(HL)] => 2 )
How to break it and echo as like this:-
2(6), 3(6(HL)), 1(5), 2(7(HL))
I have try to use implod开发者_如何学编程e to break it as a string, but this is what result I get:-
2, 3, 1, 2
any idea on this?
Thanks for advance.
assume your array is $arr :
$output = '';
foreach($arr as $k => $v) {
$output .= $v . '(' . $k . ')' . ', ';
}
$output = substr($output, 0, strlen($output)-2);
echo $output;
$s = implode(', ', array_map(function($a, $b) {
return "$b($a)";
}, array_keys($a), array_values($a)));
Or
$s = '';
foreach ($a as $key => $val)
{
if ($s) $s .= ', ';
$s .= "$val($key)";
}
精彩评论