PHP array_multisort 未按预期使用 double 多维数组进行排序

PHP array_multisort not sorting with double multidimensional array as expected

这是我的代码。

<?php

$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");

foreach ($data as $row) {
    foreach ($row as $k){
        foreach ($k as $key => $value){    
            ${$key}[]  = $value; 
        } 
    }
  }

array_multisort($pkg_version, SORT_DESC, $data);

echo "<pre>";
print_r($data);
echo "</pre>";

?>

我正在尝试使用 array_multisort 函数对多维数组进行排序

我想对每个要排序的元素pkg_version进行排序

退回的订单与预期不符。不确定,我误解了如何

array_multisort 有效吗?或者我的代码是错误的。你们能帮帮我吗?我试着

解决这个问题很久了。这是一个相当复杂的维度数组。

这是 运行 上述代码后的结果。

Array
(
    [test1] => Array
        (
            [0] => Array
                (
                    [total] => 67
                    [edition] => 2
                    [pkg_version] => 2.5.0
                )

            [1] => Array
                (
                    [toal] => 67
                    [edition] => 2
                    [pkg_version] => 0.1.0
                )

            [2] => Array
                (
                    [total] => 67
                    [edition] => 2
                    [pkg_version] => 0.3.0
                )

        )

    [test2] => Array
        (
            [0] => Array
                (
                    [total] => 86
                    [edition] => 1
                    [pkg_version] => 1.5.0
                )

            [1] => Array
                (
                    [total] => 85
                    [type] => 2
                    [pkg_version] => 0.53.0
                )

            [2] => Array
                (
                    [total] => 98
                    [type] => 2
                    [pkg_version] => 0.3
                )

            [3] => Array
                (
                    [total] => 98
                    [edition] => 2
                    [pkg_version] => 0.2
                )

        )

    [test3] => Array
        (
            [0] => Array
                (
                    [total] => 60
                    [edition] => 6
                    [pkg_version] => 0.3
                )

            [1] => Array
                (
                    [total] => 60
                    [edition] => 7
                    [pkg_version] => 0.1.1
                )

            [2] => Array
                (
                    [total] => 60
                    [edition] => 7
                    [pkg_version] => 0.25
                )

        )

)

这是我所期望的。

Array
(
    [test1] => Array
        (
            [0] => Array
                (
                    [total] => 67
                    [edition] => 2
                    [pkg_version] => 2.5.0
                )
            [1] => Array
                (
                    [total] => 67
                    [edition] => 2
                    [pkg_version] => 0.3.0
                )
            [2] => Array
                (
                    [toal] => 67
                    [edition] => 2
                    [pkg_version] => 0.1.0
                )
        )

    [test2] => Array
        (
            [0] => Array
                (
                    [total] => 86
                    [edition] => 1
                    [pkg_version] => 1.5.0
                )
            [1] => Array
                (
                    [total] => 85
                    [type] => 2
                    [pkg_version] => 0.53.0
                )
            [2] => Array
                (
                    [total] => 98
                    [type] => 2
                    [pkg_version] => 0.3
                )
            [3] => Array
                (
                    [total] => 98
                    [edition] => 2
                    [pkg_version] => 0.2
                )
        )

    [test3] => Array
        (
            [0] => Array
                (
                    [total] => 60
                    [edition] => 6
                    [pkg_version] => 0.3
                )
            [1] => Array
                (
                    [total] => 60
                    [edition] => 7
                    [pkg_version] => 0.25
                )
            [2] => Array
                (
                    [total] => 60
                    [edition] => 7
                    [pkg_version] => 0.1.1
                )
        )
)

您可以遍历数组并使用 usort()

<?php

$data['test1'][0] = array('total' => 67, 'edition' => 2, 'pkg_version' => "2.5.0" );
$data['test1'][1] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.1.0" );
$data['test1'][2] = array('total' => 67, 'edition' => 2, 'pkg_version' => "0.3.0" );
$data['test2'][0] = array('total' => 86, 'edition' => 1, 'pkg_version' => "1.5.0");
$data['test2'][1] = array('total' => 85, 'edition' => 6, 'pkg_version' => "0.53.0");
$data['test2'][2] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.3");
$data['test2'][3] = array('total' => 98, 'edition' => 2, 'pkg_version' => "0.2");
$data['test3'][0] = array('total' => 60, 'edition' => 6, 'pkg_version' => "0.3");
$data['test3'][1] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.1.1");
$data['test3'][2] = array('total' => 60, 'edition' => 7, 'pkg_version' => "0.25");

// use a reference to the sub array
//                |
//                |
//                v
foreach ($data as &$row)
{
    usort($row, function($a, $b)
                {
                    // make a desc sort by comparing $b against $a instead of $a against $b
                    return strcmp($b['pkg_version'], $a['pkg_version']);
                });
}

var_dump($data);

这输出:

array(3) {
  ["test1"]=>
  array(3) {
    [0]=>
    array(3) {
      ["total"]=>
      int(67)
      ["edition"]=>
      int(2)
      ["pkg_version"]=>
      string(5) "2.5.0"
    }
    [1]=>
    array(3) {
      ["total"]=>
      int(67)
      ["edition"]=>
      int(2)
      ["pkg_version"]=>
      string(5) "0.3.0"
    }
    [2]=>
    array(3) {
      ["total"]=>
      int(67)
      ["edition"]=>
      int(2)
      ["pkg_version"]=>
      string(5) "0.1.0"
    }
  }
  ["test2"]=>
  array(4) {
    [0]=>
    array(3) {
      ["total"]=>
      int(86)
      ["edition"]=>
      int(1)
      ["pkg_version"]=>
      string(5) "1.5.0"
    }
    [1]=>
    array(3) {
      ["total"]=>
      int(85)
      ["edition"]=>
      int(6)
      ["pkg_version"]=>
      string(6) "0.53.0"
    }
    [2]=>
    array(3) {
      ["total"]=>
      int(98)
      ["edition"]=>
      int(2)
      ["pkg_version"]=>
      string(3) "0.3"
    }
    [3]=>
    array(3) {
      ["total"]=>
      int(98)
      ["edition"]=>
      int(2)
      ["pkg_version"]=>
      string(3) "0.2"
    }
  }
  ["test3"]=>
  &array(3) {
    [0]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(6)
      ["pkg_version"]=>
      string(3) "0.3"
    }
    [1]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(7)
      ["pkg_version"]=>
      string(4) "0.25"
    }
    [2]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(7)
      ["pkg_version"]=>
      string(5) "0.1.1"
    }
  }
}

请注意,在最后一个数组中,版本0.3 高于版本0.25。因为它是您预期输出中的顺序,所以我将其保留原样,但如果不是,您可以使用 strcmp()version_compare() 代替,这将为 $data['test3'] 提供此输出:

  ["test3"]=>
  &array(3) {
    [0]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(7)
      ["pkg_version"]=>
      string(4) "0.25"
    }
    [1]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(6)
      ["pkg_version"]=>
      string(3) "0.3"
    }
    [2]=>
    array(3) {
      ["total"]=>
      int(60)
      ["edition"]=>
      int(7)
      ["pkg_version"]=>
      string(5) "0.1.1"
    }
  }
}