tags:

views:

93

answers:

4

I have this array:

Array ( [#LFC] => 1 [#cafc] => 2 [#SkySports] => 1)

How do i display it like this on a page? (preferably in value descending order as below):

\#cafc (2), #LFC (1), #SkySports (1)

Thanks

A: 

if i got your question right, use a foreach loop in combination with arsort:

arsort($array);
foreach($array as $k => $v) {
  printf('%s (%s)',
    htmlspecialchars($k),
    htmlspecialchars($v));
}
knittl
+2  A: 

Try using arsort to sort by descending value and then looping through the array, printing key/value pairs, as follows:

arsort($original_array);
foreach($original_array as $k => $v) {
  echo $k.'('.$v.')';
}
justinbach
+5  A: 

First, sort the array

arsort($arrayName);

Next, iterate througth the array keys and values.

foreach($arrayName as $key => $value)
{
    echo "$key ($value),";
}
Kristoffer S Hansen
+1 nicely explained
dnagirl
A: 
arsort($array);
$output = array();
foreach($array as $k => $v) {
   $output[] = "$k ($v)";
}
print implode(", ", $output);

this will sort the array in reverse order, then create a new array with the data formatted the way you like, then implodes the output into a string separated by commas. The other answers so far will leave a dangling comma.

Ty W
thanks for the advice
Steven