Php - how do I display elements from an array with their values
4 answers
Sort the array first
arsort($arrayName);
Next, repeat the steps of the array and values .
foreach($arrayName as $key => $value)
{
echo "$key ($value),";
}
+5
a source to share
If I understand your question correctly, use foreach
loop in combination with arsort
:
arsort($array);
foreach($array as $k => $v) {
printf('%s (%s)',
htmlspecialchars($k),
htmlspecialchars($v));
}
0
a source to share
arsort($array);
$output = array();
foreach($array as $k => $v) {
$output[] = "$k ($v)";
}
print implode(", ", $output);
this will sort the array in reverse order and then create a new array with the data formatted however you like and then inject the output on a comma separated string. The rest of the answers so far will leave a dangling comma.
0
a source to share