1

I have the following array:

array(10) { 
        [0]=> array(109) {  
            ["id"]=> string(4) "2632", ["_category_sortorder"] => 8, ["_sortorder" => 1] 
        },
        [1]=> array(109) {  
            ["id"]=> string(4) "2635", ["_category_sortorder"] => 5, ["_sortorder" => 2] 
        },
        ...
}

I want to sort it based on two criterias:

a) by _category_sortorder asc (with priority)

b) by _sortorder asc

I tried this:

foreach($resources as $k => $v) {
    $sort[$k]['_category_sortorder'] = $resources[$k]['_category_sortorder'];
    $sort[$k]['_sortorder'] = $resources[$k]['_sortorder'];
 }

array_multisort($sort['_category_sortorder'], SORT_ASC, $sort['_sortorder'], SORT_ASC, $resources);

But it's not working as expected. Any suggestions?

2
  • try usort() and define sorting behavior Commented Aug 11, 2015 at 8:46
  • Maybe try flip of arrays array_multisort($sort['_sortorder'], SORT_ASC, $sort['_category_sortorder'], SORT_ASC, $resources); Commented Aug 11, 2015 at 8:53

2 Answers 2

1

Try like this,

$sort = array(
                array("id"=>"263", "_category_sortorder"=> 8, "_sortorder" => 1),
                array( "id"=>  "145", "_category_sortorder" => 155, "_sortorder" => 2),
                array( "id"=>  "2145", "_category_sortorder" => 55, "_sortorder" => 12),
                array( "id"=>  "3145", "_category_sortorder" => 155, "_sortorder" => 10),
            );

usort($sort, function(array $a, array $b) {
    return $b['_category_sortorder'] - $a['_category_sortorder'];
});


echo '<pre>';
print_r($sort);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works for _category_sortorder, but what about _sortorder? Call usort again?
Yes if you want to sort it with _sortorder , you need to do again .but keep in mind keep the array sorted by _category_sortorder in a temp array , else it will be lost
0

I think there is a simple solution but this code block is doing is job: You split the array in sub arrays including only elements with the same _category_sortorder value. Then you sort each sub array with usort. At the end you merge them together.

<?php
    $array = [
        ['id' => 1, '_category_sortorder' => 2, '_sortorder' => 1],
        ['id' => 2, '_category_sortorder' => 2, '_sortorder' => 3],
        ['id' => 3, '_category_sortorder' => 3, '_sortorder' => 19],
        ['id' => 4, '_category_sortorder' => 1, '_sortorder' => 2],
        ['id' => 5, '_category_sortorder' => 1, '_sortorder' => 1],
    ];

    foreach ($array as $value) {
        if (!isset($newElements[$value['_category_sortorder']]))
            $newElements[$value['_category_sortorder']] = [];
        $newElements[$value['_category_sortorder']][] = $value;
    }


    $array = [];
    foreach ($newElements as $key => $value) {
        usort($value, function($a, $b) {
            return strcmp($a["_sortorder"], $b["_sortorder"]);
        });
        $array[$key] = $value;
    }

    ksort($array);
    $new = [];
    foreach ($array as $value) {
        $new = array_merge($new, $value);
    }
    echo "<pre>".print_r($new, true).'</pre>';

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.