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

  • Home
  • SEARCH
  • 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 791563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:51:26+00:00 2026-05-14T21:51:26+00:00

I have an array of products $products = array_count_values($products); now I have an array

  • 0

I have an array of products

$products = array_count_values($products);

now I have an array where $key is product number and $value is how many
times I have such a product in the array.
I want to sort this new array that product with the least “duplicates” are
on the first place, but what ever I use (rsort, krsort,..) i loose product
numbers (key).

any suggestions?

thanks.

  • 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-14T21:51:27+00:00Added an answer on May 14, 2026 at 9:51 pm

    Take a look at arsort() as an alternative to rsort() (and that family of functions).

    Generally, the ‘Sorting arrays‘ page on php.net might be useful to you – it’s a comparison of PHP’s array sorting functions based on what they sort, what direction they sort in, and whether they maintain the keys while sorting.


    Mind you, for the sake of completion:

    Going by ‘now I have an array where $key is product number and $value is how many times I have such a product in the array. I want to sort this new array that product with the least “duplicates” are on the first place‘, you probably want asort() (the pendant to sort()).


    Your comment example, using asort():

    $arr = array(
        1 => 3,
        2 => 2,
        5 => 3,
        9 => 1
    );
    asort($arr);
    print_r($arr);
    

    yields:

    Array
    (
        [9] => 1
        [2] => 2
        [1] => 3
        [5] => 3
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this really large array being created and displayed, and right now it
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
i have a array of strings i want to write a query such that
I have a products array of widgets. Some widgets have the reserved period symbol
I have an array of products IDs and I need to find the IDs
I have a object (product), with a property of type 'array' e.g. product.tags =
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
i have an array that can contain the same value more than once. What
For eg I have this code on the main page. <?php session_start(); $_SESSION['order']=array(); ?>
I have the following code $result = $handle->select()->from('store_products_id', array('count'=>'COUNT(store_products_id.product_id)')) ->where('store_products_id.store_id=?', $this->store_id) ->columns($selectColumns) ->join('product_quickinfo', 'store_products_id.product_id

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.