I’ve got poll results stored in a mysql database.
I try to output the results which go’s well, but now I try to get the result in descending order (highest first) and with the right name. Now the output is like this:
print "<pre>";
print_r(array_count_values($array_a));
print "</pre>";
//OUTPUTS first key is the poll option and second is how much it was voted for.
[4] => 1
[12] => 17
[2] => 3
[6] => 42
[8] => 6
[16] => 5
[3] => 30
[18] => 1
[1] => 5
First I like to replace the numbers with a name. This is where I got stuck. str_replace doesn’t work cause it replaces all numbers matching but not the exact number. The foreach loops get it right but there a 17 numbers to be replaced so I like to use an array to get them from but don’t know how.
foreach($array_a as &$value){
if($value==1){
$value = "opt1";
}
}
$patterns = array();
$patterns[0] = '1';
$patterns[1] = '2';
$patterns[2] = '3';
$patterns[3] = '4';
$replacements = array();
$replacements[0] = 'Car';
$replacements[1] = 'Boat';
$replacements[2] = 'Bike';
$replacements[3] = 'Photo';
The result I like to achieve:
//OUTPUT
[Car] => 30
[Bike] => 25
[Paint] => 10
[Goat] => 5
[Photo] => 3
How about:
A very practical example would be:
and this outputs me:
is it as expected?