0

I have a multi dimension array like below:

Array 
   (
   [1200] => Array 
      (
      [B] => Array
         (
         [4] => Array
             (
             [Name] => 'Joe']
             )
         )
      [A] => Array
         (
         [3] => Array
             (
             [Name] => 'Paul']
             )
         )
      )
   [1100] => Array 
      (
      [F] => Array
         (
         [2] => Array
             (
             [Name] => 'Sam']
             )
         )
      [D] => Array
         (
         [1] => Array
             (
             [Name] => 'Jane']
             )
         )
      )

What I wish to achieve is having the 4 digit number 1100 and 1200 in order ascending, then I need the letters (B A) and (F D) also in order, and then the single digit number under them in order ascending too. I believe I'm looking at a multi dimension array but any help would be appreciated.

1

1 Answer 1

1

The below function might be what you're looking for. It recursively orders arrays by their key.

function ksort_r(&$array) {
   foreach ($array as &$value) {
      if (is_array($value)) {
          ksort_r($value);
      }
   }
   return ksort($array);
}

Example usage

function ksort_r(&$array) {
   foreach ($array as &$value) {
      if (is_array($value)) {
          ksort_r($value);
      }
   }
   return ksort($array);
}

$data = [
    1200 => [
        'B' => [
            4 => [
                'Name' => 'Joe'
            ]
        ],
        'A' => [
            3 => [
                'Name' => 'Paul'
            ]
        ]
    ],
    1100 => [
        'F' => [
            2 => [
                'Name' => 'Sam'
            ]
        ],
        'D' => [
            1 => [
                'Name' => 'Jane'
            ]
        ]
    ]
];

ksort_r($data);

print_r($data);

The above will output...

Array
(
    [1100] => Array
        (
            [D] => Array
                (
                    [1] => Array
                        (
                            [Name] => Jane
                        )

                )

            [F] => Array
                (
                    [2] => Array
                        (
                            [Name] => Sam
                        )

                )

        )

    [1200] => Array
        (
            [A] => Array
                (
                    [3] => Array
                        (
                            [Name] => Paul
                        )

                )

            [B] => Array
                (
                    [4] => Array
                        (
                            [Name] => Joe
                        )

                )

        )

)
Sign up to request clarification or add additional context in comments.

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.