views:

87

answers:

4

hi to all!

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.

+2  A: 

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
)
pinkgothic
Hi thanx for quick reply i have checked arsort() but not returning the required result any suggestions??
umermalik
@umermalik: Just making sure I understand what you're saying: You get the correct result with `rsort()` (excepting the keys), but not with `arsort()`?
pinkgothic
`arsort()` sorts from *highest* to *lowest*, but the OP wants from *lowest* to *highest*. Hence `asort()` is the proper function to use.
Felix Kling
@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
@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 ;)
Felix Kling
Edited, given that answers should strive for completion. :)
pinkgothic
+1 for that. :)
Felix Kling
@Felix Kling: Probably. But he did quote `rsort()`. I have the bad habit of asking questions when all I want to do is make people think about *their* question. ;)) Thanks.
pinkgothic
asort() arsort() keep keys and values association intact
Kamil Szot
Guys i am going to show you the real data what i am getting and what i want it to be$array = array('1','2','5','9','5','1','2','1','5');$new_array = array_count_values($array);print_r($new_array);returnsArray ( [1] => 3 [2] => 2 [5] => 3 [9] => 1 )i want to sort data by the value in desc order it should come[9] => 1[2] => 2[1] => 3[5] => 3thanx
umermalik
@umermalik: Yes, and `asort()` will do that. Is it not working for you?
pinkgothic
@pinkgothic: i used it but it sort the key not the value i need to sort according to value and lowest to hiest
umermalik
The key and the value both are numeric is that making any problem??
umermalik
@umermalik: I'm quite confused. I tested your data and it gave me the right result with `asort()`. What you describe sounds like `ksort()`. Hold on, I'll share.
pinkgothic
+1  A: 

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.

Felix Kling
+1 for a pretty nifty, thorough explanation of `asort()`, plus caveats.
pinkgothic
+1  A: 

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

Alix Axel
A: 

asort()

Mark Baker