2

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

1

4 Answers 4

5

First, sort the array

arsort($arrayName);

Next, iterate througth the array keys and values.

foreach($arrayName as $key => $value)
{
    echo "$key ($value),";
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.')';
}

Comments

0

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));
}

Comments

0
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.

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.