0

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 implode to break it as a string, but this is what result I get:-

2, 3, 1, 2

any idea on this?

Thanks for advance.

1
  • I assume you mean the output should be 1(6), 3(6(HL)), 1(5), 2(7(HL)). Commented Jul 15, 2011 at 7:16

2 Answers 2

5

assume your array is $arr :

$output = '';
foreach($arr as $k => $v) {
  $output .= $v . '(' . $k . ')' . ', ';
}
$output = substr($output, 0, strlen($output)-2);
echo $output;
Sign up to request clarification or add additional context in comments.

1 Comment

You can use rtrim($output, ', ');
0
$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)";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.