亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果數組有重復項,則鍵的空值

如果數組有重復項,則鍵的空值

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 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號