为了账号安全,请及时绑定邮箱和手机立即绑定

如果数组有重复项,则键的空值

如果数组有重复项,则键的空值

PHP
青春有我 2022-10-28 15:10:13
我有这个数组    Array(    [0] => Array        (            [user_id] => 78            [post_id] => 3            [post_user_added_id] => 2        )    [1] => Array        (            [user_id] => 76            [post_id] => 3            [post_user_added_id] => 16        )    [2] => Array        (            [user_id] => 78            [post_id] => 3            [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         )我想要做的是如果密钥post_id重复我只想清空它并保留一个所以我的最终数组将如下所示 Array(    [0] => Array        (            [user_id] => 78            [post_id] => 3            [post_user_added_id] => 2        )    [1] => Array        (            [user_id] => 76            [post_id] =>             [post_user_added_id] => 16        )    [2] => Array        (            [user_id] => 78            [post_id] =>             [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] =>              [post_user_added_id] => 15         )我已经尝试过这段代码,但它似乎不起作用它删除了整个数组                           foreach($arry as $k => $v)                            {                                foreach($arry as $key => $value)                                {                                    if($k != $key && $v['post_id'] == $value['post_id'])                                    {                                        unset($arry [$k]);                                    }                                }                            }                            print_r($arry);
查看完整描述

4 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

您可以foreach与ternary操作员一起执行


$last = null;//this will keep the previous post_id

foreach($arr as &$v){

 ($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);

}

print_r($arr);

工作示例:- https://3v4l.org/RiU9J


查看完整回答
反对 回复 2022-10-28
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

使用您的数组值而不是 $arr 并尝试像以下代码一样过滤数组:


 $arr = array(

        array(

            'user_id'=>15,

            'post_id'=>3,

            'post_user_added_id'=>2

            ),


            array(

            'user_id'=>16,

            'post_id'=>3,

            'post_user_added_id'=>2

            ),


         array(

            'user_id'=>18,

            'post_id'=>3,

            'post_user_added_id'=>2

            ),


             array(

            'user_id'=>18,

            'post_id'=>3,

            'post_user_added_id'=>2

            ),


             array(

            'user_id'=>16,

            'post_id'=>3,

            'post_user_added_id'=>2

            ),

    );


    $postids = array();

    foreach($arr as $key=>$val){


        if(in_array($val['post_id'], $postids)){ 

            $arr[$key]['post_id'] = '';

        }else{

            $postids[] = $val['post_id'];

        }

    }


    echo "<pre>";print_r($arr);exit;    

输出 :


Array

(

    [0] => Array

        (

            [user_id] => 15

            [post_id] => 3

            [post_user_added_id] => 2

        )


    [1] => Array

        (

            [user_id] => 16

            [post_id] => 

            [post_user_added_id] => 2

        )


    [2] => Array

        (

            [user_id] => 18

            [post_id] => 

            [post_user_added_id] => 2

        )


    [3] => Array

        (

            [user_id] => 18

            [post_id] => 

            [post_user_added_id] => 2

        )


    [4] => Array

        (

            [user_id] => 16

            [post_id] => 

            [post_user_added_id] => 2

        )


)


查看完整回答
反对 回复 2022-10-28
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

尝试这个 :


// example code

$arrayResult = array();

$arry = [

    1 =>

        [

            'user_id' => 78,

            'post_id' => 3,

            'post_user_added_id' => 2

        ],


    2=>

        [

            'user_id' => 76,

            'post_id' => 3,

            'post_user_added_id' => 16

        ],

    3 =>

        [

            'user_id' => 78,

        'post_id' => 3,

            'post_user_added_id' => 12

        ],


    4 =>

        [

            'user_id' => 76,

            'post_id' => 9,

            'post_user_added_id' => 15

        ],


     5 =>

         [

            'user_id' => 77,

            'post_id' => 9,

            'post_user_added_id' => 15

         ]];


$final = array();

foreach($arry as $a)

{

    if (in_array($a['post_id'], $arrayResult)) {

        $a['post_id'] = 0;

    }else{

        $arrayResult[] = $a['post_id'];

    }

    $final[] = $a;

}


var_dump($final);


查看完整回答
反对 回复 2022-10-28
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

在这里试试这个。


$tempArr = [];

$resArr = [];

foreach($orignalArr as $key=>$obj){

    if(in_array($obj['post_id'], $tempArr)){

        $obj['post_id'] = '';

        $resArr[] = $obj;

    }else{

        $tempArr[] = $obj['post_id'];

        $resArr[] = $obj;

    }

}


print_r($resArr);


查看完整回答
反对 回复 2022-10-28
  • 4 回答
  • 0 关注
  • 136 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号