PHP Array removing the duplicate key value and displaying only one

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

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

The idea here is that when there is a duplicate user_id, it will just display only one? This is the expected result:

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

The reason why [2] key instead of [0] key or [1] key instead of [3] is displayed because I want to get the bottom key of the duplicate key. It's kinda hard to explain but I hope you understood the scenario or the output that I expected.

Your help would be greatly appreciated! Thanks! :)

回答1:

Try this :

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


回答2:

Try

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

Output

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 


回答3:

You can use a foreach loop to overwrite the user_id. I think something like this should work

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!