-2

I have an array as shown below. I want to sort it with the srp value in ascending order. How can I do this in PHP. I am a beginner I think it can be done using usort() but am not sure how to do it.

Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1900
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1000
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => JIVI X525(Red)
            [mrp] => 0
            [srp] => 1250
        )

)

Desired Output:

Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => JIVI X525(Red)
            [mrp] => 0
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1900
        )
)

My Class looks like this:

class TopDealsDAOMongoImpl implements TopDealsDAO{
    //put your code here
    public function getTopDeals() {
        $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));
        function my_sort($a,$b)
        {
            if ($a->srp == $b->srp) return 0;
            return ($a->srp < $b->srp)?-1:1;
        }

        uasort($topDeals, 'my_sort'); 

    }


}
6

4 Answers 4

2

Try below code, I hope it is helpful.

PHP Script.

<?php
    // Static array.
    $array=array();
    $array[0]=array(
        '_id'=>'5911af8209ed4456d069b1d3',
        'title'=>'Zen M4S(Silver) 1',
        'srp'=>1900
    );
    $array[1]=array(
        '_id'=>'5911af8209ed4456d069b1d2',
        'title'=>'Zen M4S(Silver) 2',
        'srp'=>1000
    );
    $array[2]=array(
        '_id'=>'5911af8209ed4456d069b1d4',
        'title'=>'Zen M4S(Silver) 1',
        'srp'=>1250
    );

    // For acending sorting.
    function asc_sort_json($array1,$array2){
        $on = 'srp';
        if ($array1[$on] == $array2[$on]) {
            return 0;
        }
        return ($array1[$on] < $array2[$on]) ? -1 : 1;
    }
    // For decending sorting.
    function desc_sort_json($array1,$array2){
        $on = 'srp';
        if ($array1[$on] == $array2[$on]) {
            return 0;
        }
        return ($array1[$on] < $array2[$on]) ? 1 : -1;
    }

    $array_json = json_encode($array); // encode array in json
    $array_json1 = json_decode($array_json); // decode json from array because get json object
    $array_json2 = json_decode(json_encode($array_json1),true); // parse json into array

    usort($array_json2, "asc_sort_json"); // Call function for acending order.
    $array_json2 = json_decode(json_encode($array_json2)); // Array to json.

    $array_json1 = json_encode($array); // encode array in json
    $array_json2 = json_decode($array_json1); // decode json from array because get json object
    $array_json3 = json_decode(json_encode($array_json2),true); // parse json into array

    usort($array_json3, "desc_sort_json"); // Call function for decending order.
    $array_json4 = json_decode(json_encode($array_json2)); // Array to json.
?>

Outputs.

echo "<pre>";
print_r($array_json2);
echo "<pre>";
print_r($array_json4);

// Asc 
Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver) 1
            [srp] => 1900
        )

)

// Desc
Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver) 1
            [srp] => 1900
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

Give this a try:

// Sort array
uasort($array, 'cmp');

function cmp($a, $b) {
   if ($a['srp'] == $b['srp']) {
      return 0;
   }
   // Return in acsending order
   return ($a['srp'] < $b['srp']) ? -1 : 1;
}

Read more: Uasort

4 Comments

[ErrorException] uasort() expects parameter 2 to be a valid callback, function 'my_sort' not found or invalid function name
Can you post the code that returned that error here? my_sort was not in my example.
also tried yours: uasort($topDeals, 'cmp'); var_dump($topDeals); function cmp($a, $b) { if ($a['srp'] == $b['srp']) { return 0; } // Return in acsending order return ($a['srp'] < $b['srp']) ? -1 : 1; }
0

For simple array

function my_sort($a,$b)
{
    if ($a['srp'] == $b['srp']) return 0;
    return ($a['srp'] < $b['srp'])?-1:1;
}

uasort($array, 'my_sort'); // replace $array with your variable

For StdObject syntax will be:

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

uasort($array, 'my_sort'); // replace $array with your variable

4 Comments

[ErrorException] uasort() expects parameter 2 to be a valid callback, function 'my_sort' not found or invalid function name
have you added my_sort function and also changed name of $array to your array name?
then you should have a look on this link :stackoverflow.com/questions/38228477/…
also check how to call function : stackoverflow.com/questions/1725165/…
-1

use usort and pass user define function in it.

$tmp =  new stdClass;
$tmp->_id="5911af8209ed4456d069b1d3";
$tmp->title="Zen M4S(Silver)";
$tmp->mrp=0;
$tmp->srp=1900;

$tmp1 =  new stdClass;
$tmp1->_id="5911af8209ed4456d069b1d2";
$tmp1->title="Zen M4S(Silver)";
$tmp1->mrp=0;
$tmp1->srp=1000;

$tmp2 =  new stdClass;
$tmp2->_id="5911af8209ed4456d069b1d4";
$tmp2->title="JIVI X525(Red)";
$tmp2->mrp=0;
$tmp2->srp=1250;

$new_array = array($tmp,$tmp1,$tmp2);
function sort_arr($a, $b)
{
    return ($a->srp<$b->srp)?-1:1;
}

usort($new_array, "sort_arr");
var_dump($new_array);

From your edit you are using class, So you can do it like this:

class TopDealsDAOMongoImpl implements TopDealsDAO{
    //put your code here
    public function getTopDeals() {
        $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));


        uasort($topDeals, array($this, "sort_arr")); 

    }
    public function sort_arr($a,$b)
    {
        if ($a->srp == $b->srp) return 0;
        return ($a->srp < $b->srp)?-1:1;
    }


}

o/p:

array (size=3)
  0 => 
    object(stdClass)[2]
      public '_id' => string '5911af8209ed4456d069b1d2' (length=24)
      public 'title' => string 'Zen M4S(Silver)' (length=15)
      public 'mrp' => int 0
      public 'srp' => int 1000
  1 => 
    object(stdClass)[3]
      public '_id' => string '5911af8209ed4456d069b1d4' (length=24)
      public 'title' => string 'JIVI X525(Red)' (length=14)
      public 'mrp' => int 0
      public 'srp' => int 1250
  2 => 
    object(stdClass)[1]
      public '_id' => string '5911af8209ed4456d069b1d3' (length=24)
      public 'title' => string 'Zen M4S(Silver)' (length=15)
      public 'mrp' => int 0
      public 'srp' => int 1900

3 Comments

[ErrorException] Invalid argument supplied for foreach()
It must be diffrent thing. In this q/a there is no foreach loop
usort($topDeals, function($a, $b) { return $b->srp - $a->srp; }); var_dump($topDeals); this worked

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.