0

In the array below you can see that the order of 'listElemId' differs from the three first elements in the array. What I want is them to be in order (listElem 1,2,3). 'listElemPoints' should of course follow.

How can this be accomplished?

Thanks in advance!

$listArray {
  [0] {
      [0] {
          ["listElemId"] = 2    // <- should be 1
          ["listElemPoints"] = 1 }
      [1] {
          ["listElemId"] = 3    // <- should be 2
          ["listElemPoints"] = 2 }
      [2] {
        ["listElemId"] = 1      // <- should be 3
        ["listElemPoints"] = 3 }
      }
  [1] {
      [0] {
          ["listElemId"] = 3    // <- should be 1
          ["listElemPoints"] = 1 }
      [1] {
          ["listElemId"] = 2    // <- correct
          ["listElemPoints"] = 2 }
      [2] {
          ["listElemId"] = 1    // <- should be 3
          ["listElemPoints"] = 3 }
      }
  [2] {
      [0] {
          ["listElemId"] = 3    // <- should be 1
          ["listElemPoints"] = 1 }
      [1] {
          ["listElemId"] = 1    // <- should be 2
          ["listElemPoints"] = 2 }
      [2] {
          ["listElemId"] = 2    // <- should be 3
          ["listElemPoints"] = 3 }
      }
}

1 Answer 1

1

Something like this should work (untested):

foreach ($listArray as &$arr) {
  uasort($arr, function($a, $b) {
    return $a['listElemId'] > $b['listElemId'];
  });
}
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.