【问题标题】:Sorting multidimensional array in third level在第三级对多维数组进行排序
【发布时间】:2012-10-19 05:21:12
【问题描述】:

在下面的数组中,您可以看到“listElemId”的顺序与数组中的前三个元素不同。我想要的是它们按顺序排列(listElem 1,2,3)。 'listElemPoints' 当然应该跟在后面。

如何做到这一点?

提前致谢!

$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 }
      }
}

【问题讨论】:

    标签: php arrays sorting multidimensional-array associative-array


    【解决方案1】:

    这样的东西应该可以工作(未经测试):

    foreach ($listArray as &$arr) {
      uasort($arr, function($a, $b) {
        return $a['listElemId'] > $b['listElemId'];
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2010-10-13
      相关资源
      最近更新 更多