0


i have a problem.
i want to sort array follow max value in colum feed_id with every user_id, then loop.
this is array start :

$array_start = array(
        array("user_id"=>1,"feed_id"=>10),
        array("user_id"=>1,"feed_id"=>11),
        array("feed_id"=>2,"user_id"=>25),
        array("feed_id"=>9,"user_id"=>26),
        array("feed_id"=>2,"user_id"=>30),
        array("feed_id"=>7,"user_id"=>33),  
);

then sort array_start, i want to return array_result :

$array_result = array(
        //loop 1
        array("user_id"=>1,"feed_id"=>37),
        array("feed_id"=>2,"user_id"=>30),
        array("feed_id"=>9,"user_id"=>26),
        array("feed_id"=>7,"user_id"=>33),
        //loop 2
        array("user_id"=>1,"feed_id"=>11),
        array("feed_id"=>2,"user_id"=>25),      
);

please help me, thank your reading !

user3714098
  • 25
  • 1
  • 4
  • Are you getting the data from a MySql query. If Yes, then you can sort it in your query as well ! – ssadaqat Jul 03 '15 at 08:16
  • http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value. Alternatively, if you have your array in a data base, you can use the order by clause. Select * from table order by user_id, feed_id – user3538411 Jul 03 '15 at 08:18
  • are u sure with you $array_start array? it does not match the result where is this arary: array("user_id"=>1,"feed_id"=>37) – Amani Ben Azzouz Jul 03 '15 at 08:23
  • @Amani i forget delete that line :)), array("user_id"=>1,"feed_id"=>10) => array("user_id"=>1,"feed_id"=>37) – user3714098 Jul 03 '15 at 08:36
  • @ssadaqat please, can you write some code for me ? – user3714098 Jul 03 '15 at 08:40

1 Answers1

0

Here you go!

I am writing the query based on a random table name. Please change it accordingly.

 select user_id, max(feed_id) from MyTable
 group by user_id
 order by feed_id DESC;

The above query will result both the columns and the sorting will be done on feed_id and in descending order.

ssadaqat
  • 158
  • 6
  • oh no ! that isn't my result i need, please see $array_result, i have user [1,2,9,7], when first loop, return max value in colum feed_id with corresponding user_id. i also use user[1,2,9,7] again, this is second loop, return max value in colum feed_id except max values in first loop. :))) that is my problem ! – user3714098 Jul 03 '15 at 10:41
  • Your explanation is a bit confusing but I have edited my query once more. Have a look. There are no loops but you'll get the maximum feed_id for every user_id you have in the table. Hope that helps! – ssadaqat Jul 03 '15 at 12:30