-2

重複の可能性:
多次元配列内の要素の比較

私の配列は次のようになります

Array
(
    [0] => Array
        (
            [event_key] => 1
            [event_id] => 5
            [event_desc] => Test Event 1
        )

    [1] => Array
        (
            [event_key] => 2
            [event_id] => 2
            [event_desc] => Test Event 2
        )

    [2] => Array
        (
            [event_key] => 3
            [event_id] => 5
            [event_desc] => Test Event 3
        )

    [3] => Array
        (
            [event_key] => 4
            [event_id] => 4
            [event_desc] => Test Event 4

        )

    [4] => Array
        (
            [event_key] => 5
            [event_id] => 5
            [event_desc] => Test Event 5

        )

)

event_id が重複している場合、サブ配列を削除したい

だから望ましい結果は

Array
(
    [0] => Array
        (
            [event_key] => 1
            [event_id] => 5
            [event_desc] => Test Event 1
        )

    [1] => Array
        (
            [event_key] => 2
            [event_id] => 2
            [event_desc] => Test Event 2
        )

    [2] => Array
        (
            [event_key] => 4
            [event_id] => 4
            [event_desc] => Test Event 4

        )


)
4

1 に答える 1

1

このような何かがうまくいくでしょう:

$knownIds = array();
foreach( $myArray AS $key=>$item ) {
  if( array_key_exists($item['event_id'], $knownIds) === true ) {
    unset( $myArray[$key] );
  } else {
    $knownIds[$item['event_id']] = $key;  // value does not matter really here
  }
}
$myArray = array_values($myArray);

$myArrayソース配列はどこにありますか。

于 2012-08-31T11:02:01.367 に答える