3
Array
(
    [0] => Array
        (
            [user_id] => 78
            [post_id] => 3
            [post_user_added_id] => 2
        )

    [1] => Array
        (
            [user_id] => 76
            [post_id] => 8
            [post_user_added_id] => 16
        )

    [2] => Array
        (
            [user_id] => 78
            [post_id] => 9
            [post_user_added_id] => 12
        )

    [3] => Array
        (
            [user_id] => 76
            [post_id] => 9
            [post_user_added_id] => 15
        )

     [4] => Array
         (
             [user_id] => 77
             [post_id] => 9
             [post_user_added_id] => 15
         )

)

ここでの考え方は、重複するuser_idがある場合、1つだけを表示するということです。これは期待される結果です:

 Array
 (
      [2] => Array
          (
             [user_id] => 78
             [post_id] => 9
             [post_user_added_id] => 12
           )

        [3] => Array
            (
                [user_id] => 76
                [post_id] => 9
                [post_user_added_id] => 15
            )

         [4] => Array
             (
                 [user_id] => 77
                 [post_id] => 9
                 [post_user_added_id] => 15
             )

    )

重複キーの一番下のキーを取得したいので、[0]キーの代わりに[2]キーまたは[3]の代わりに[1]キーが表示される理由。説明するのはちょっと難しいですが、私が期待したシナリオや出力を理解していただければ幸いです。

あなたの助けをいただければ幸いです!ありがとう!:)

4

4 に答える 4

7

これを試して :

foreach($arr as $k => $v) 
{
    foreach($arr as $key => $value) 
    {
        if($k != $key && $v['user_id'] == $value['user_id'])
        {
             unset($arr[$k]);
        }
    }
}
print_r($arr);
于 2012-04-24T15:38:44.663 に答える
1

試す

$array = Array (
        "0" => Array (
                "user_id" => 78,
                "post_id" => 3,
                "post_user_added_id" => 2 
        ),

        "1" => Array (
                "user_id" => 76,
                "post_id" => 8,
                "post_user_added_id" => 16 
        ),

        "2" => Array (
                "user_id" => 78,
                "post_id" => 9,
                "post_user_added_id" => 12 
        ),

        "3" => Array (
                "user_id" => 76,
                "post_id" => 9,
                "post_user_added_id" => 15 
        ),

        "4" => Array (
                "user_id" => 77,
                "post_id" => 9,
                "post_user_added_id" => 15 
        ) 
);
$keys = array ();

// Get Position
foreach ( $array as $key => $value ) {
    $keys [$value ['user_id']] = $key;
}

// Remove Duplicate
foreach ( $array as $key => $value ) {
    if (! in_array ( $key, $keys )) {
        unset ( $array [$key] );
    }
}
var_dump ( $array );

出力

array
  2 => 
    array
      'user_id' => int 78
      'post_id' => int 9
      'post_user_added_id' => int 12
  3 => 
    array
      'user_id' => int 76
      'post_id' => int 9
      'post_user_added_id' => int 15
  4 => 
    array
      'user_id' => int 77
      'post_id' => int 9
      'post_user_added_id' => int 15
于 2012-04-24T15:38:47.393 に答える
0

foreachループを使用してuser_idを上書きできます。私はこのようなものがうまくいくはずだと思います

$new = array();
foreach ($array as $value)
{
    $new[$value['user_id']] = $value;
}
print_r($new);
于 2012-04-24T15:41:24.813 に答える
0

以下は私のために働いた、以下を掃除するために:

$arr = array(

    array("ID"=>"234"),
    array("ID"=>"235"),
    array("ID"=>"236"),
    array("ID"=>"236"),
    array("ID"=>"234"),
    );

コード:

for ($i=0; $i <count($arr) ; $i++) {

    $distinct_id = $arr[$i]["ID"]; // this is the ID for which we want to eliminate the duplicates
    $position = $i; // position of that array (so we ignore it)
    $unset_arr = array(); // these duplicate entries will be removed

    // Collect list of duplicates to remove
    for ($y=0; $y <count($arr) ; $y++) {

        // Skip the position of the array that we're checking (we want to remove duplicate not the original)
        if($y == $position)
            continue;

        // If found remove and reset keys
        if($arr[$y]["ID"] == $distinct_id) {
            $unset_arr[] = $y;
        }

    }

    // Remove IDs collected in the loop above
    foreach ($unset_arr as $k) {
        unset($arr[$k]);
    }

    // Reset keys
    $arr = array_values($arr);

}

(古いものではなく新しい重複を削除する場合は、配列を逆にします)

于 2019-01-05T23:49:50.910 に答える