Y
Y
Yuri Pikhtarev2014-08-07 12:43:31
PHP
Yuri Pikhtarev, 2014-08-07 12:43:31

How to sort a multidimensional array by two parameters at once?

Please tell me how to sort an array like this:

array(3) {
    ["beta"]=> array(4) {
      ["b"]=> string(1) "..."
      ["c"]=> string(1) "..."
      ["a"]=> string(1) "..."
      ["d"]=> string(1) "..."
    }
    ["alpha"]=> array(4) {
      ["d"]=> string(1) "..."
      ["c"]=> string(1) "..."
      ["b"]=> string(1) "..."
      ["a"]=> string(1) "..."
    }
    ["gamma"]=> array(4) {
      ["c"]=> string(1) "..."
      ["d"]=> string(1) "..."
      ["a"]=> string(1) "..."
      ["b"]=> string(1) "..."
    }

In this one:
array(3) {
    ["alpha"]=> array(4) {
      ["a"]=> string(1) "..."
      ["b"]=> string(1) "..."
      ["c"]=> string(1) "..."
      ["d"]=> string(1) "..."
    }
    ["beta"]=> array(4) {
      ["a"]=> string(1) "..."
      ["b"]=> string(1) "..."
      ["c"]=> string(1) "..."
      ["d"]=> string(1) "..."
    }
    ["gamma"]=> array(4) {
      ["a"]=> string(1) "..."
      ["b"]=> string(1) "..."
      ["c"]=> string(1) "..."
      ["d"]=> string(1) "..."
    }
  }

I tried to figure out the uasort function, but it didn't work because of the unknown names of the array elements. Many thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1001001 111, 2014-08-07
@Exileum

Well, you can use the ksort function;

ksort($array);
foreach($array as $key=>$value) {
    ksort($array[$key]);
}

V
VyusFire, 2014-08-07
@VyusFire

What about nested loops?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question