beanymanuk Posted November 20, 2017 Share Posted November 20, 2017 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 More sharing options...
requinix Posted November 21, 2017 Share Posted November 21, 2017 If the question is a custom sort then the answer is probably usort. Link to comment Share on other sites More sharing options...
beanymanuk Posted November 21, 2017 Author Share Posted November 21, 2017 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 More sharing options...
requinix Posted November 21, 2017 Share Posted November 21, 2017 Don't you mean ->name? Link to comment Share on other sites More sharing options...
Barand Posted November 21, 2017 Share Posted November 21, 2017 Also return $a - $b;That will work for numeric values but in this case you need return strcmp($a,$b); Link to comment Share on other sites More sharing options...
beanymanuk Posted May 17, 2018 Author Share Posted May 17, 2018 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 More sharing options...
Barand Posted May 17, 2018 Share Posted May 17, 2018 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.