1

Consider this multidimensional array:

array(4) {
  [0]=>
  array(2) {
    ["number1"]=>
    int(100)
    ["number2"]=>
    int(24)
  }
  [1]=>
  array(2) {
    ["number1"]=>
    int(100)
    ["number2"]=>
    int(22)
  }
  [2]=>
  array(2) {
    ["number1"]=>
    int(300)
    ["number2"]=>
    int(30)
  }
  [3]=>
  array(2) {
    ["number1"]=>
    int(200)
    ["number2"]=>
    int(23)
  }
}

I need this array to be sorted ascending by the key 'number1'. If the number1 values of multiple arrays are the same, the array needs to be sorted ascending by the key 'number2'. Eventually, this needs to be the outcome:

array(4) {
  [0]=>
  array(2) {
    ["number1"]=>
    int(100)
    ["number2"]=>
    int(22)
  }
  [1]=>
  array(2) {
    ["number1"]=>
    int(100)
    ["number2"]=>
    int(24)
  }
  [2]=>
  array(2) {
    ["number1"]=>
    int(200)
    ["number2"]=>
    int(23)
  }
  [3]=>
  array(2) {
    ["number1"]=>
    int(300)
    ["number2"]=>
    int(30)
  }
}

I have read the PHP manual about the array_multisort() function and read some stackoverflow posts about this, but i cannot make anything of it.

2 Answers 2

3

You can do it using array_multisort(). Your code should be :

    $arr = array(
      array(
        "number1"=>100,
        "number2"=>24
      ),
      array(
        "number1"=>100,
        "number2"=>22
      ),
      array(
        "number1"=>300,
        "number2"=>30
      ),
      array(
        "number1"=>200,
        "number2"=>23
      ));
// Make a $tempArr for list of sort columns and their data to pass to array_multisort function  
    $tempArr = array();

    foreach($arr as $key=>$val) {
        $tempArr['number1'][$key] = $val['number1'];
        $tempArr['number2'][$key] = $val['number2'];
    }
// sort by number1 asc and then number2 asc
    array_multisort($tempArr['number1'], SORT_ASC, $tempArr['number2'], SORT_ASC,$arr);

    print_r($arr);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this should work. I have tested it with my sample array and I got the desired output. I'm not sure how array_multisort() actually works though.
Glad to help you.. :)
0

What about sorting it through usort

$array = array( array('number' => 100, 'test' => 30),array('number' => 100, 'test' => 20),  array('number' => 40), array('number' => 60));
usort($array, "custom_sort");
// Define the custom sort function
function custom_sort($a,$b) {
  return $a['number'] > $b['number'];
}

Output

echo '<pre>';print_r($array );
Array
(
    [0] => Array
        (
            [number] => 40
        )

    [1] => Array
        (
            [number] => 60
        )

    [2] => Array
        (
            [number] => 100
            [test] => 20
        )

    [3] => Array
        (
            [number] => 100
            [test] => 30
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.