-1
array(1) {
  [0]=>
  array(1) {
    ["12345"]=>
    array(1) {
      ["orange"]=>
      string(46) "test.jpg"
      ["blue"]=>
      string(46) "test2.jpg"
      ["green"]=>
      string(46) "test3.jpg"
    }
  }
}

I want to sort it by key:

foreach ($array as $key => $value) {
      if(is_array($value)){
           foreach ($value as $k => $v) {
                usort($v);
                foreach ($v as $fileIterator => $fileData) {
                     echo $fileIterator;
                }
           }
      }
 }

This is the result

orange
blue
green

But I expect the order to be

blue
green
orange
peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

2

Try this, You have to sort based on key so use ksort(). While using foreach() the duplicate of array is processed. In order to make changes in original array use '&'(calling by reference)

foreach ($array as $key => $value) {
      if(is_array($value)){
           foreach ($value as $k => &$v) {
                ksort($v);
                foreach ($v as $fileIterator => $fileData) {
                     echo $fileIterator;
                }
           }
      }
 }
Abhishek
  • 1,008
  • 1
  • 16
  • 39