1

I got ONE array which contains multiple array and a value with order which contains 1 2 3 4 5 6 7 8 etc ....

I can't change this structure

["Categ1"]=>
 array(3) {
   ["order"]=>
   string(1) "5"
   [0]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
   [1]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
 }
 ["Categ2"]=>
 array(3) {
   ["order"]=>
   string(1) "2"
   [0]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
   [1]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
 }

What I need is pretty simple but I can't find a solution :

["Categ2"]=>
array(3) {
  ["order"]=>
  string(1) "2"
  [0]=>
  array(5) {
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    NULL
  }
  [1]=>
  array(5) {
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    string(x) "xxx"
    ["content"]=>
    NULL
  }
}
["Categ1"]=>
 array(3) {
   ["order"]=>
   string(1) "5"
   [0]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
   [1]=>
   array(5) {
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     string(x) "xxx"
     ["content"]=>
     NULL
   }
 }

Just a simple order with the order value.

0

1 Answer 1

1

You need to define a custom sorting function and hand it over to a php sort function. e.g. uasort

This minimalistic example shows it pretty good.

$cats = [
    'cat1' => [
        "order" => 5,
        ["foo" => "bar"]
    ],
    'cat2' => [
        "order" => 3,
        ["foo" => "bar"]
    ]
];

uasort($cats, function($a, $b){
    return $a['order'] > $b['order'];
});

var_dump($cats);

Will output:

 array (size=2)   
 'cat2' => 
    array (size=2)
      'order' => int 3
      0 => 
        array (size=1)
          'foo' => string 'bar' (length=3)   
 'cat1' => 
    array (size=2)
      'order' => int 5
      0 => 
        array (size=1)
          'foo' => string 'bar' (length=3)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.