-2

I have an associative array in PHP like:

    $newarray1 = Array (
    [productA] => Array (
        [link] => http://sanjosespartan.com/blog/products/agile-java-development/
        [visitcount] => 4
        )
    [productD] => Array (
        [link] => http://sanjosespartan.com/blog/products/intro-to-go/
        [visitcount] => 1
        )
    [productG] => Array (
        [link] => http://sanjosespartan.com/blog/products/node-js-for-developers/
        [visitcount] => 1
        )
    [productB] => Array (
        [link] => http://sanjosespartan.com/blog/products/beginning-mysql/
        [visitcount] => 1
        )
    [productC] => Array (
        [link] => http://sanjosespartan.com/blog/products/computer-networks/
        [visitcount] => 1
        )
    [productF] => Array (
        [link] => http://sanjosespartan.com/blog/products/mongo-database/
        [visitcount] => 1
        )
    [productH] => Array (
        [link] => http://sanjosespartan.com/blog/products/python/
        [visitcount] => 1
        )
    [productI] => Array (
        [link] => http://sanjosespartan.com/blog/string-theory/
        [visitcount] => 1
        )
    [productJ] => Array (
        [link] => http://sanjosespartan.com/blog/tcs/
        [visitcount] => 1
        )
    [productE] => Array (
        [link] => http://sanjosespartan.com/blog/products/java-complete-refrence/
        [visitcount] => 2
        )
)

Now I need to sort the entire array based on the visit count. I tried using arsort but it did not work.

Tom Zych
  • 12,329
  • 9
  • 33
  • 51
  • 1
    http://php.net/manual/en/function.array-multisort.php – ckimbrell Oct 02 '15 at 20:37
  • You tagged it with `usort`, and that is also the right function ot use. Check out the [PHP manual](http://hk2.php.net/manual/en/function.usort.php) (example #2), it describes how to do it. – Anders Oct 02 '15 at 20:39
  • your code has too many errors like key are constants which will show undefined, you values are strings but with out quotes, you are using array as elements but with out commas. – Prafulla Kumar Sahu Oct 02 '15 at 20:48
  • Your table would be a whole lot shorter if you left out the constant stuff like `http://sanjosespartan.com/blog/products/` and added it in code. – Tom Zych Nov 14 '15 at 21:57

2 Answers2

0

You'll need a custom callback function that compares two outer elements by checking out their ["visitcount"] field. This can be done with usort and if you have a modern version of php you can do it with an unnamed ad-hoc function (you don't need a named one).

usort is simple: give it an array and a function that tells which of its two parameters is greater. Something like:

if($a["visitcount"]>$b["visitcount"]) return 1;
if($a["visitcount"]<$b["visitcount"]) return -1;
return 0;

Hope this helps.

dkellner
  • 5,301
  • 1
  • 31
  • 36
0

There's already a lot of answers for this. I like this one :

https://stackoverflow.com/a/16788610/4632883

Look harder before posting !

Community
  • 1
  • 1
Flo
  • 79
  • 1
  • 3