0

I need to sort multiple array by one value in array. Unfortunately, I can not sort this inside the database. I can only get array like this.

My Array look like :

Array
(
    [0] => Array
        (
            [id] => 10913
            [name] => NAME 1
            [logo_img] => 28361bd6114a32daafcf150da5ee1d33.jpeg
            [has_frame] => t
            [weight] => 0
        )

    [1] => Array
        (
            [id] => 10902
            [name] => Name 2
            [logo_img] => d0642d5efeabe8b861f2f32fa17f35b3.jpeg
            [has_frame] => t
            [weight] => 4
        )

    [2] => Array
        (
            [id] => 8887
            [name] => Some name 3
            [logo_img] => 12e47533604438bf390d69218434b630.jpeg
            [has_frame] => f
            [weight] => 0
        )

    [3] => Array
        (
            [id] => 49
            [name] => Some name 4
            [logo_img] => 5971148b049c5bb0b6e402a1de1836ef.jpeg
            [has_frame] => t
            [weight] => 0
        )

    [4] => Array
        (
            [id] => 10871
            [name] => name2
            [logo_img] => 7dcc1a6058a81b4e45de5f2274025907.jpeg
            [has_frame] => t
            [weight] => 0
        )

    [5] => Array
        (
            [id] => 1880
            [name] => name 4
            [logo_img] => dc05764ea71425a4a653b81997e7d929.jpeg
            [has_frame] => f
            [weight] => 0
        )

    [6] => Array
        (
            [id] => 9678
            [name] => name 33
            [logo_img] => 8ded98244a6928d5179398fa9d3b59bc.jpeg
            [has_frame] => t
            [weight] => 5
        )

    [7] => Array
        (
            [id] => 2880
            [name] => name r
            [logo_img] => 09876329a5ac2a84e9f83923c75e780c.jpeg
            [has_frame] => f
            [weight] => 0
        )

    [8] => Array
        (
            [id] => 8265
            [name] => name 5
            [logo_img] => 487ed3c676666e380f813f5f1a9fb040.jpeg
            [has_frame] => t
            [weight] => 1
        )

)

I need to order this array by weight element DESC. Is it possible?

5
  • 1
    yes it is possible. what have you tried so far? Commented Sep 10, 2014 at 11:23
  • See stackoverflow.com/questions/17364127/… Commented Sep 10, 2014 at 11:24
  • I try array_multisort(), but no effect. Commented Sep 10, 2014 at 11:25
  • This could help as well. Commented Sep 10, 2014 at 11:25
  • stackoverflow.com/questions/2699086/… this can help you Commented Sep 10, 2014 at 11:26

3 Answers 3

2

http://php.net/manual/en/function.sort.php

This lists a lot of information on sorting,

if you need to sort using the key, try

ksort($array)
Sign up to request clarification or add additional context in comments.

Comments

1

this is how I did it:

usort($object, 'cmp');

function cmp($a, $b)
{
    return strcmp($b['weigth'], $a['weigth']); // or -> weigth
}

Comments

1
 //You can use `uasort` that has a comparison function that can be defined by user;


//if the name of the whole array is $array

//define a function 

 function compare($obj1,$obj2){
     if($obj1['weight']==$obj2['weight']){
         return 0;
     }
  return $obj1['weight']<$obj2['weight']?1:-1;

}
//the comparison operator in the function `compare` in the second return determines the order of the array elements.


 //Now call the function with your array

 uasort($array,'compare');

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.