11

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.

7 Answers 7

14

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
)
Sign up to request clarification or add additional context in comments.

13 Comments

Hi thanx for quick reply i have checked arsort() but not returning the required result any suggestions??
@umermalik: Just making sure I understand what you're saying: You get the correct result with rsort() (excepting the keys), but not with arsort()?
arsort() sorts from highest to lowest, but the OP wants from lowest to highest. Hence asort() is the proper function to use.
@Felix Kling: Yeah, it was my first assumption, too, but the emphasis was on the lost keys. 'what ever I use (rsort, krsort,..) i loose product numbers (key)'. So I figured I'd mention the family and general way to solve the key-loss problem rather than focus on what might be an erroneous question. But you're right.
@pinkgothic: I mean, I am just guessing that this is what the OP means with not returning the required result. Could also be something else ;)
|
4

You want to use asort():

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.


rsort() was wrong from the first place anyway (and so are any other sorting functions that have the r (for reverse) in it), as it would sort the array from highest to lowest.

asort() sorts from lowest to highest:

<?php
$array = array('f'=>1, 'a'=>2, 'c'=>5);  
asort($array);
print_r($array);

gives

Array
(
    [f] => 1
    [a] => 2
    [c] => 5
)

Note: These functions sort the arrays in-place. They do not return a sorted array. The return values is:

(..) TRUE on success or FALSE on failure.

1 Comment

+1 for a pretty nifty, thorough explanation of asort(), plus caveats.
3

Try using asort() or arsort() or any other sort function that maintains index association.

Comments

0

You should use asort() PHP function.

Comments

0

asort() is array sort, not ascending sort.

dsort doesn't exist. The function you are looking for is arsort() - array reverse sort.

https://www.php.net/manual/en/function.asort.php

https://www.php.net/manual/en/function.arsort.php

Comments

0

Use this to sort by [values]:

$array = array_count_values($array);
ksort($array, SORT_REGULAR);

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1

Just a thought; asort - sorts ascending (low to high)

perhaps try

dsort - descending (high to low)

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.