0

I have an array:

[0] => Array
    (
        [Id] => 1
        [Order] => 1
        [ContentGroupId] => 10
        [ContentGroupIsNew] => 0
 )

[1] => Array
    (
        [Id] => 2
        [Order] => 2
        [ContentGroupId] => 11
        [ContentGroupIsNew] => 0
 )
[2] => Array
    (
        [Id] => 3
        [Order] => 3
        [ContentGroupId] => 12
        [ContentGroupIsNew] => 1
 )
[3] => Array
    (
        [Id] => 4
        [Order] => 4
        [ContentGroupId] => 13
        [ContentGroupIsNew] => 1
 )
[4] => Array
    (
        [Id] => 5
        [Order] => 5
        [ContentGroupId] => 14
        [ContentGroupIsNew] => 0
 )

The default order is by [Order] I want to re-sort this array so that it orders by [ContentGroupIsNew] = 1 at the top (if any exist, in this sample 2 do exist), but keep the existing order of the remainder of elements.

If I use a usort function, I can get the [ContentGroupIsNew] = 1 but then the remainder of elements seems to get randomly ordered and not keep true to the original [Order] value.

So the final result should look like this:

[0] => Array
    (
        [Id] => 3
        [Order] => 3
        [ContentGroupId] => 12
        [ContentGroupIsNew] => 1
 )

[1] => Array
    (
        [Id] => 4
        [Order] => 4
        [ContentGroupId] => 13
        [ContentGroupIsNew] => 1
 )
[2] => Array
    (
        [Id] => 1
        [Order] => 1
        [ContentGroupId] => 10
        [ContentGroupIsNew] => 0
 )
[3] => Array
    (
        [Id] => 2
        [Order] => 2
        [ContentGroupId] => 11
        [ContentGroupIsNew] => 0
 )
[4] => Array
    (
        [Id] => 5
        [Order] => 5
        [ContentGroupId] => 14
        [ContentGroupIsNew] => 0
 )

PHP code:

function sort_array($b, $a) {
  return $a['ContentGroupIsNew'] - $b['ContentGroupIsNew'];
}

usort($array, 'sort_array');
Jeff Solomon
  • 459
  • 6
  • 21

2 Answers2

1
foreach ($data as $key => $row) {
    $return_fare[$key]  = $row['ContentGroupIsNew'];
    $one_way_fare[$key] = $row['Id'];
}


array_multisort($return_fare, SORT_DESC, $one_way_fare, SORT_ASC, $data);
print_r($data);

found this solution here : PHP sort array by two field values

Community
  • 1
  • 1
Saba Tandashvili
  • 117
  • 2
  • 11
  • This got me close, but the examples from that other solution [PHP sort array by two field values](http://stackoverflow.com/questions/4582649/php-sort-array-by-two-field-values) got me there. Thx. – Jeff Solomon Feb 17 '17 at 08:50
  • Glad to help, how current sorting code looks like ? just interested what did you change. – Saba Tandashvili Feb 17 '17 at 08:54
  • `usort($app_iaps, function($a, $b) { $rdiff = $b['ContentGroupIsNew'] - $a['ContentGroupIsNew']; if ($rdiff) return $rdiff; return $a['Order'] - $b['Order']; });` – Jeff Solomon Feb 17 '17 at 08:55
0

please try this

   function myCmp($a, $b)
    {
      return strcmp($b["ContentGroupIsNew"], $a["ContentGroupIsNew"]);
    }

    uasort($array, "myCmp")

echo "<pre>"; print_r($array);