【问题标题】:PHP Array removing the duplicate key value and displaying only onePHP Array 删除重复的键值并只显示一个
【发布时间】:2019-01-07 12:47:52
【问题描述】:
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时,它只会显示一个?这是预期的结果:

 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
             )

    )

之所以显示[2]键而不是[0]键或[1]键而不是[3]是因为我想获取重复键的底部键。这有点难以解释,但我希望你能理解我所期望的场景或输出。

您的帮助将不胜感激!谢谢! :)

【问题讨论】:

  • 到目前为止你尝试过什么? - 请发布您的代码,并明确说明您遇到了障碍。
  • 我没有尝试任何东西,因为我真的不知道执行预期输出的逻辑。
  • 这个网站是为程序员准备的,所以请在此处查看常见问题解答。
  • 您是否考虑过将数组移入数据库?
  • 我是一名程序员,这就是我想出 PHP 数组值的原因。我只是问如何删除数组上的重复值。

标签: php multidimensional-array


【解决方案1】:

试试这个:

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

【讨论】:

  • 您离开的前一个运行良好!不需要 array_reverse() 它,因为我想获取重复键值的底部键。 :)
【解决方案2】:

试试

$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

【讨论】:

  • 您可能需要 array_reverse 初始数组,因为这不是所需的输出。
【解决方案3】:

您可以使用 foreach 循环覆盖 user_id。我认为这样的事情应该可以工作

$new = array();
foreach ($array as $value)
{
    $new[$value['user_id']] = $value;
}
print_r($new);

【讨论】:

    【解决方案4】:

    以下对我有用,用于清理以下内容:

    $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);
    
    }
    

    (如果要删除较新的重复项而不是较旧的重复项,请反转数组)

    【讨论】:

      猜你喜欢
      • 2022-07-10
      • 2011-02-22
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2017-07-29
      • 2013-04-23
      • 2020-06-22
      • 2022-08-17
      相关资源
      最近更新 更多