Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6251477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:37:22+00:00 2026-05-24T13:37:22+00:00

I’ve got poll results stored in a mysql database. I try to output the

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:37:23+00:00Added an answer on May 24, 2026 at 1:37 pm

    How about:

    $replacements = array();
    $replacements[2] = 'Car';   // your key should be the key of $array_a here and 
    $replacements[1] = 'Boat';  // value should be the key you want to be used
    $replacements[5] = 'Bike';
    $replacements[3] = 'Photo';
    
    
    $finalPollArray = array();
    
    foreach($replacements as $key => $value)
    {
        $finalPollArray[$value] = $array_a[$key];
    }  
    
    $finalPollArray = asort($finalPollArray)
    
    print "<pre>";
    print_r($finalPollArray);
    print "</pre>";
    

    A very practical example would be:

    $poll[5] = 13;
    $poll[6] = 12;
    $poll[3] = 10;
    $poll[12] = 7;
    $poll[8] = 6;
    $poll[1] = 5;
    $poll[7] = 5;
    $poll[16] = 5;
    $poll[13] = 4;
    
     // with this code I get the following output
    $replacements = array();
    $replacements[5] = 'Car';
    
    $finalPollArrayA = array();
    
     foreach($replacements as $key => $value)
     {
         $finalPollArrayA[$value] = $poll[$key];
     }
    
     print "<pre>";
     print_r($finalPollArrayA);
     print "</pre>";
    

    and this outputs me:

    Array
    (
        [Car] => 13
    )
    

    is it as expected?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.