-4

我有以下两个数组:

[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
                )

        )

请问,有人可以帮我吗?非常感谢您!:)

4

2 回答 2

2

The is a very simple task that can be achieved with a simple loop (Don't be afraid of loops)

Step 1 : Merge and Pad Array

// 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;
    }
}

Step 2: Sort and Output array

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

// Output Result
print_r($new);

See Live DEMO

于 2013-06-09T18:30:39.663 回答
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;
});

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

于 2013-06-09T10:13:09.617 回答