对两个或多个数组进行排序和合并

时间:2013-06-07 09:06:40

标签: php arrays

我有以下两个数组:

[BA] => Array
    (
        [0] => Array
            (
                [num] => 55
                [val] => 1
            )

        [1] => Array
            (
                [num] => 41
                [val] => 5
            )

        [2] => Array
            (
                [num] => 65
                [val] => 27
            )

        [3] => Array
            (
                [num] => 60
                [val] => 28
            )

        [4] => Array
            (
                [num] => 70
                [val] => 90
            )

        [5] => Array
            (
                [num] => 78
                [val] => 113
            )

        [6] => Array
            (
                [num] => 85
                [val] => 119
            )

    )



[GE] => Array
    (
        [0] => Array
            (
                [num] => 76
                [val] => 1
            )

        [1] => Array
            (
                [num] => 26
                [val] => 6
            )

        [2] => Array
            (
                [num] => 69
                [val] => 11
            )

        [3] => Array
            (
                [num] => 64
                [val] => 48
            )

        [4] => Array
            (
                [num] => 9
                [val] => 85
            )

        [5] => Array
            (
                [num] => 36
                [val] => 112
            )

        [6] => Array
            (
                [num] => 90
                [val] => 127
            )

    )

我需要按“val”对上述两个数组进行降序排序,然后将它们合并为一个数组,如下所示:

 [new] => Array
        (
            [0] => Array
                (
                    [ruota] => 'GE'
                    [num] => 90
                    [val] => 127
                )

            [1] => Array
                (
                    [ruota] => 'BA'
                    [num] => 85
                    [val] => 119
                )

            [2] => Array
                (
                    [ruota] => 'BA'
                    [num] => 78
                    [val] => 113
                )

            [3] => Array
                (
                    [ruota] => 'GE'
                    [num] => 36
                    [val] => 112
                )

            [4] => Array
                (
                    [ruota] => 'BA'
                    [num] => 70
                    [val] => 90
                )

            [5] => Array
                (
                    [ruota] => 'GE'
                    [num] => 9
                    [val] => 85
                )

            [6] => Array
                (
                    [ruota] => 'GE'
                    [num] => 64
                    [val] => 48
                )

        )

拜托,有人可以帮帮我吗? 非常感谢你提前! :)

2 个答案:

答案 0 :(得分:2)

这是一个非常简单的任务,可以通过一个简单的循环来实现(不要害怕循环)

第1步:合并和填充数组

// Merge Both arrays
$array = $ba + $ge; // or array_merge($ba,$ge);

// New Array
$new = [];

// Start Loop to pad array
foreach($array as $ruota => $var) {
    foreach($var as $v) {
        $v['routa'] = $ruota; // add routa to each element
        $new[] = $v;
    }
}

第2步:排序和输出数组

// Sort Values
usort($new, function ($a, $b) {
    return $b['val'] - $a['val']; // Sort Decending
});

// Output Result
print_r($new);

See Live DEMO

答案 1 :(得分:1)

首先将密钥添加到每个数组,例如类似的东西:

foreach (array($ba, $ge) as &$array) {
  foreach ($array as $key => &$values) {
    foreach ($values as &$value) {
      $value['ruota'] = $key;
    }
  }
}

然后合并,例如:

$values = array_merge(array_values($ba), array_values($ge));

然后排序,例如:

usort($values, function($a, $b) {
  if ($a['val'] == $b['val']) {
        return 0;
  }
  return ($a['val'] < $b['val']) ? -1 : 1;
});

注意:上面可能有一两个错误,但它应该让你走上正轨。