Jump to content

Custom Sort an array by object key


beanymanuk

Recommended Posts

Hi

 

How can I custom sort this array into any order I specify by say the object key - name like this

P2,P1,P3

 

or by id

sku991,sku838,sku123,

 

Here is a PHP var dump
 

array(3) {
  [0]=>
  object(stdClass)#212 (2) {
    ["id"]=>
    string(36) "sku838"
    ["name"]=>
    string(61) "P1"
  }
  [1]=>
  object(stdClass)#235 (2) {
    ["id"]=>
    string(36) "sku991"
    ["name"]=>
    string(61) "P2"
  }
  [2]=>
  object(stdClass)#240(2) {
    ["id"]=>
    string(36) "sku123"
    ["name"]=>
    string(61) "P3"
  }
}

Thankyou

Link to comment
Share on other sites

Trying this but it's not working? Not sure where I am going wrong

$order = array(
    'P2',
    'P1',
    'P3'
);

function custom_compare($a, $b){
    global $order;
    //echo $a->id."</br>";
    $a = array_search($a->id, $order);
    //echo $a."</br>";
    $b = array_search($b->id, $order);
    if($a === false && $b === false) { // both items are dont cares
        //echo "HERE1";
        return 0;                      // a == b
    }
    else if ($a === false) {           // $a is a dont care item
        //echo "HERE2";
        return 1;                      // $a > $b
    }
    else if ($b === false) {           // $b is a dont care item
        //echo "HERE3";
        return -1;                     // $a < $b
    }
    else {
        //echo "HERE4";
        return $a - $b;
    }
}
//shuffle($products);  // for testing
//var_dump($products); // before
usort($products, "custom_compare");
//var_dump($products); // after
Link to comment
Share on other sites

  • 5 months later...
  function custom_compare($a, $b){
      global $order;
      //echo $a->id."</br>";
      $a = array_search($a->name, $order);
      //echo $a."</br>";
      $b = array_search($b->name, $order);
      if($a === false && $b === false) { // both items are dont cares
          //echo "HERE1";
          return 0;                      // a == b
      }
      else if ($a === false) {           // $a is a dont care item
          //echo "HERE2";
          return 1;                      // $a > $b
      }
      else if ($b === false) {           // $b is a dont care item
          //echo "HERE3";
          return -1;                     // $a < $b
      }
      else {
          //echo "HERE4";
          return strcmp($a,$b);
      }
  }
  //shuffle($products);  // for testing
  //var_dump($products); // before
  usort($categories, "custom_compare");
  //var_dump($products); // after

So I have this, but this isn't working any idea's what is wrong?

Link to comment
Share on other sites

try

 

$data = [   (object)[ 'id' => 'sku838',
                      'name' => 'P1'  ],
            (object)[ 'id' => 'sku991',
                      'name' => 'P3'  ],         
            (object)[ 'id' => 'sku123',
                      'name' => 'P2'  ]
        ];

$sortBy = 'name';      // define the property to sort by

usort($data, function($a, $b) use ($sortBy) {
                  return strcmp($a->$sortBy, $b->$sortBy) ;
                  });
                  
echo '<pre>', print_r($data, 1), '</pre>';        
Link to comment
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.