Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multi dimensional array like:

[
   {
      "C":[
         {
            "status":"0",
            "num":"3223"
         },
         {
            "status":"1",
            "num":"45186"
         },
         {
            "status":"2",
            "num":"8310"
         },
         {
            "status":"3",
            "num":"82"
         }
      ]
   },
   {
      "F":[
         {
            "status":"0",
            "num":"1506"
         },
         {
            "status":"1",
            "num":"31253"
         },
         {
            "status":"2",
            "num":"1660"
         },
         {
            "status":"5",
            "num":"1017"
         }
      ]
   },
   {
      "A":[
         {
            "status":"0",
            "num":"1506"
         },
         {
            "status":"1",
            "num":"31253"
         },
         {
            "status":"2",
            "num":"1660"
         },
         {
            "status":"5",
            "num":"1017"
         }
      ]
   },
]

I want to sort this based on the keys (C, F, A) etc. I thought about writing my own selection sort kind of sorting method using foreachs, but I am sure it's not a very good way to do. I can also use ksort(), usort(), but not sure how.

I don't need anyone to write the whole code for me please, I will appreciate guidance about what is the best way to go about sorting an array like this.

The above string is actually an json_encode() dump of the array.

Here is the var_dump():

array(6) { [0] => array(1) { 'C' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } } [1] => array(1) { 'F' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } } [2] => array(1) { 'A' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } } [3] => array(1) { 'D' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } } [4] => array(1) { 'E' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } } [5] => array(1) { 'B' => array(8) { [0] => array(2) { ... } [1] => array(2) { ... } [2] => array(2) { ... } [3] => array(2) { ... } [4] => array(2) { ... } [5] => array(2) { ... } [6] => array(2) { ... } [7] => array(2) { ... } } }}

Above's pretty print

array(6) {
  [0] =>
  array(1) {
    'C' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
  [1] =>
  array(1) {
    'F' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
  [2] =>
  array(1) {
    'A' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
  [3] =>
  array(1) {
    'D' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
  [4] =>
  array(1) {
    'E' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
  [5] =>
  array(1) {
    'B' =>
    array(8) {
      [0] =>
      array(2) {
        ...
      }
      [1] =>
      array(2) {
        ...
      }
      [2] =>
      array(2) {
        ...
      }
      [3] =>
      array(2) {
        ...
      }
      [4] =>
      array(2) {
        ...
      }
      [5] =>
      array(2) {
        ...
      }
      [6] =>
      array(2) {
        ...
      }
      [7] =>
      array(2) {
        ...
      }
    }
  }
}
share|improve this question
 
Not sure, I tried, it cannot get inside the third level or so. –  John Smith Oct 29 at 15:07
 
I don't even know if you need to use multisort. Wouldn't you just ksort the top-level array? It should sort the top level keys by alpha. The result would be A, C, F in that case. Unless the original question wasn't defined enough, that should sort the array properly. OP, is the desire to sort the top-level, and then sort by status? –  jkinz Oct 29 at 15:09
 
@jkinz Do you mean ksort($whateverArray)? It doesn't work for me for some reason. Though it really should. –  John Smith Oct 29 at 15:19
 
Can you var_dump the actual array? If the keys are really A, C, F it should work. The very first example on the ksort doc page is alpha sort, so I'm wondering if there is something else going on here. –  jkinz Oct 29 at 15:22
 
@jkinz I pasted the var dump, I do see something goofy there. –  John Smith Oct 29 at 15:28
show 4 more comments

1 Answer

up vote 1 down vote accepted

Here is how I accomplished this. There is probably a better way, but we need to create a new array in memory with the alpha-characters as the actual keys (basically we are removing the parent arrays we don't need). Then we use ksort to actually sort the array.

<?php 
$test = array(
    array("a" => array("status" =>1, "blah" => 2)),
    array("f" => array("status" =>1, "blah" => 2)),
    array("c" => array("status" =>1, "blah" => 2)),
    array("b" => array("status" =>1, "blah" => 2)),
    array("z" => array("status" =>1, "blah" => 2))
);
foreach($test as $key=>$val){
    foreach($val as $key2=>$val2){
        $newTest[$key2] = $val2;
    }
}
echo '<pre>';
var_dump($test);
ksort($newTest);
var_dump($newTest);
echo '</pre>';
?>
share|improve this answer
 
Thank you, that worked! :) –  John Smith 2 days ago
 
It probably isn't the most efficient, so if you have a lot of objects you may want to think about ways to optimize it, but if you're working with a small dataset it shouldn't be too impactful to performance. Glad it worked for you! –  jkinz 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.