1

$list$list2の2 つの配列があります。

$リスト:

Array
(
[0] => Array
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 2
    )

[1] => Array
    (
        [team] => 1
        [team_points] => 3
        [team_occurrences] => 2
    )

[2] => Array
    (
        [team] => 5
        [team_points] => 1
        [team_occurrences] => 1
    )

[3] => Array
    (
        [team] => 13
        [team_points] => 1
        [team_occurrences] => 1
    )

[4] => Array
    (
        [team] => 7
        [team_points] => 0
        [team_occurrences] => 1
    )

[5] => Array
    (
        [team] => 2
        [team_points] => 0
        [team_occurrences] => 3
    )

 )

$list2:

Array
(
    [0] => Array
        (
            [team] => 20
            [team_points] => 7
            [team_occurrences] => 3
        )

    [1] => Array
        (
            [team] => 10
            [team_points] => 3
            [team_occurrences] => 1
        )

    [2] => Array
        (
            [team] => 14
            [team_points] => 3
            [team_occurrences] => 1
        )

    [3] => Array
        (
            [team] => 13
            [team_points] => 3
            [team_occurrences] => 1
        )

    [4] => Array
        (
            [team] => 19
            [team_points] => 3
            [team_occurrences] => 1
        )

    [5] => Array
        (
            [team] => 17
            [team_points] => 1
            [team_occurrences] => 1
        )

    [6] => Array
        (
            [team] => 11
            [team_points] => 0
            [team_occurrences] => 1
        )

    [7] => Array
        (
            [team] => 15
            [team_points] => 0
            [team_occurrences] => 1
        )

)

ご覧のとおり、列は両方の配列で同じです (team、team_points、team_occurrences)

ここで、これら 2 つの配列を$list_allという配列にマージしたいと思います。

マージの問題は、私が試した標準のマージです

array_merge($list,$list2);

それらを一緒に追加するだけです。

ただし、必要なのは同じチームをカウントすることです。たとえば、 [team] => 14[team] => 13は両方の配列 ($list と $list2) にあるため、 $list からteam_points列の値を合計する必要があります。チームが同一の場合、$ list2 のteam_points列の値を使用します。team_occurrences列についても同じです。

だから例えば

新しい配列は次のようにはなりません。

Array
(
[0] => Array // from $list
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 2
    )

[1] => Array // from $list2
    (
        [team] => 14
        [team_points] => 3
        [team_occurrences] => 1
    )

[3] => Array // from $list
    (
        [team] => 13
        [team_points] => 1
        [team_occurrences] => 1
    )

[4] => Array // from $list2
    (
        [team] => 13
        [team_points] => 3
        [team_occurrences] => 1
    )

しかし、私はそれが次のようになる必要があります:

Array
(
[0] => Array 
    (
        [team] => 14
        [team_points] => 6
        [team_occurrences] => 3
    )

[1] => Array 
    (
        [team] => 13
        [team_points] => 4
        [team_occurrences] => 2
    )

マージ後、usort() または team_points DESC によるより良い関数 (最高値から最低値へ) を使用して結果配列をソートしたいと思います。

アドバイスをよろしくお願いします。

4

3 に答える 3

2

最初のリストを繰り返し、キーがチーム番号である新しい配列を作成します。次に、2番目の配列を繰り返し、チームがすでに存在する場合は追加し、そうでない場合は追加します。

$combined = array();
// First loop creates array keys in $combined array
// based on team numbers
foreach($list as $initial) {
  $combined[$initial['team']] = $initial; 
}

// Second loop looks at $list2 and either adds to values
// if the team key already exists, or just adds the 
// whole subarray on if it doesn't.
foreach($list2 as $secondary) {
  // If it was in the first, append to it.
  if (isset($combined[$secondary['team']])) {
    $combined[$secondary['team']]['team_points'] += $secondary['team_points'];
    $combined[$secondary['team']]['team_occurrences'] += $secondary['team_occurrences'];
  }
  // Otherwise, just add it to the array
  else {
    $combined[$secondary['team']] = $secondary;
  }
}

// Then sort the combined array with usort() into descending order by points
// Anonymous callback function is PHP 5.3+. Needs to be a string value of a named function
// for PHP < 5.3
usort($combined, function($a, $b) {
  if ($a['team_points'] === $b['team_points']) {
     return 0;
  }
  else {
     return $a['team_points'] < $b['team_points'] ? 1 : -1;
  }
});

注:上記は、チーム番号でキー設定された出力配列を生成します。本当に昇順の数字キーが必要な場合は、次のようarray_values()に呼び出します。

// Strip off the team number keys and just use ascending numbers
$combined = array_values($combined);

編集上記のいくつかの欠落を修正しました]...

于 2012-10-12T15:03:32.180 に答える
1

次の方法で実行できます。

foreach($list2 as $ky => $val) {
    $list2['t'.$val['team']] = $val;
}

$tot = count($list1);
$list3 = array();
for ($i=0;$i<$tot;$i++) {
    $team = $list1[$i]['Team'];
    $list3[]['Team'] = $team;
    $points = isset($list2['t'+$team]) ? $list2['t'+$team]['team_points'] : 0;
    $list3[]['team_points'] = $list1[i]['team_points'] + $points;
    $ocu = isset($list2['t'+$team]) ? $list2['t'+$team]['team_ocurrences'] : 0;
    $list3[]['team_ocurrences'] = $list1[$i]['team_ocurrences'] + $ocu;
}

function cmp($a, $b) {
    if ($a['team_points'] == $b['team_points']) {
        return 0;
    }
    return ($a['team_points'] > $b['team_points']) ? -1 : 1;
}

usort($list3, "cmp");
于 2012-10-12T15:16:16.527 に答える
1

最も簡単なのは、両方の配列をチーム番号でソートすることです ( を使用usort)。両方の配列を同時に見て、最も小さいチーム番号を選択します。list1 が最低の場合、そのチームを結果に追加し、list1 インデックスを増やします。list2 が最低の場合、そのチームを結果に追加し、list2 インデックスを増やします。両方のチームが同じ場合は、ポイントとオカレンスを追加してそれらを結合します。1 つのリストの最後まで繰り返し、他のリストから残りの要素をすべて追加します。

于 2012-10-12T15:07:59.567 に答える