Jump to content

Array Sort Fail


monkeytooth

Recommended Posts

I've tried various sort methods on this array. And seem to be failing.

Array
(
    [monkey quest] => 8
    [monkey] => 2
    [monkey sports] => 0
    [monkey go happy] => 0
    [monkey go happy 3] => 1
    [monkey joes] => 0
    [monkey games] => 2
    [monkey bread recipe] => 1
    [monkey go happy 2] => 3
    [monkey quest trailer] => 4
    [monkey quest guide] => 5
    [monkey quest nick] => 6
)

 

What I want to do or attempt to do rather is sort the array by the => value from highest to lowest (or maybe in reverse as well, but one thing at a time)

 

My last failed attempt I was trying out usort() as my concept..

function cmp($a, $b)
{
    if ($a == $b)
 {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}

echo "<pre>";
print_r(usort($kw, "cmp"));
echo "</pre>";

 

But the only thing I am left with on the page is just 1 everything else seems to be getting lost, or I dunno whats going on. So I think I've done stumped my self. Now I'm looking for idea's assuming I am tackling this all wrong.

 

 

Link to comment
https://forums.phpfreaks.com/topic/233684-array-sort-fail/
Share on other sites

the array I posted in my example. Thats the actual output from the overall function I have working currently. With that I am I want to sort it by those numeric values so it would go from the original output:

 

Array(
    [monkey quest] => 8
    [monkey] => 2
    [monkey sports] => 0
    [monkey go happy] => 0
    [monkey go happy 3] => 1
    [monkey joes] => 0
)

 

to this output:

Array(
    [monkey quest] => 8
    [monkey] => 2
    [monkey go happy 3] => 1
    [monkey go happy] => 0
    [monkey joes] => 0
    [monkey sports] => 0
)

 

or this output:

 

Array(
    [monkey go happy] => 0
    [monkey joes] => 0
    [monkey sports] => 0
    [monkey go happy 3] => 1
    [monkey] => 2
    [monkey quest] => 8
)

 

Link to comment
https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201449
Share on other sites

only thing I can use to get it to work is a "usort" however I lose my values

 

this is what I am doing:

function cmp($a, $b)
{
    if ($a == $b)
 {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
}
usort($kw, "cmp");

 

kw being the array I posted earlier. My Issue is after its sorted I want to take the pairs of data and list them out in that order on the page

 

trying this

foreach($kw as $key => $value)
{
echo $key . " - " . $value ."<br>";
}

 

i get:

0 - 0
1 - 0
2 - 0
3 - 1
4 - 8

 

what I want is:

monkey go happy - 0
monkey joes - 0
monkey sports - 0
monkey go happy 3 - 1
monkey quest - 8

 

Any idea's how to get that? Ive tried other sort methods to. But none work for me thus far other than usort, and usort really isnt doing what I want it to fully maybe Im doing the function its calling wrong. but I need extra eyes to help me sift through and figure it out.

Link to comment
https://forums.phpfreaks.com/topic/233684-array-sort-fail/#findComment-1201522
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.