doumangzhen7204 2016-04-29 21:03
浏览 362
已采纳

如何在php中从另一个数组中删除一个数组?

I have the following arrays in PHP: print_r($employees) =

Array
(
    [0] => Array
        (
            [p_id] => T29083999
            [name] => Robert Plaxo
        )

    [1] => Array
        (
            [p_id] => T29083388
            [name] => Yvan Sergei
        )

    [2] => Array
        (
            [p_id] => T21083911
            [name] => Rhonda Saunders
        )

    [3] => Array
        (
            [p_id] => H02910382
            [name] => Miguel Mercado
        )
)

then this array: print_r($record) =

Array
(
    [0] => Array
        (
            [c_id] => 1
            [section] => 1061
            [term] => 201631
            [p_id] => T29083388
            [c_date] => 2016-04-01 09:14:00
        )

)

I want to remove the array element from $employees that matches the p_id of $record. Array $record may have multiple entries like the one shown. If so, any p_id in $record must be removed from $employees.

I tried:

foreach ($employees as $k => $e) {
    foreach ($record as $r) {
        if ($e['p_id']==$r['p_id']) {
            echo "found it!";
            // if I uncomment the next line, it crashes! (500 server error)
            //  unset $employees[$k];
        }           
    }   
}

I just want to remove any element from $employees that has any employee that matches any record in $record with that employee id.

  • 写回答

2条回答 默认 最新

  • ds211107 2016-04-29 21:12
    关注

    You were almost there; just needed parens around your unset()

    I also took the liberty to change some of your variable names as single character variable names bother me.

    $employees[] = [
        'p_id' => 'T29083999',
        'name' => 'Robert Plaxo',
    ];
    $employees[] = [
        'p_id' => 'T29083388',
        'name' => 'Yvan Sergei',
    ];
    $employees[] = [
        'p_id' => 'T21083911',
        'name' => 'Rhonda Saunders',
    ];
    $employees[] = [
        'p_id' => 'H02910382',
        'name' => 'Miguel Mercado',
    ];
    
    $records[] = [
            'c_id' => '1',
            'section' => '1061',
            'term' => '201631',
            'p_id' => 'T29083388',
            'c_date' => '2016-04-01 09:14:00',
    ];
    
    foreach ($employees as $key => $employee) {
        foreach ($records as $record) {
            if ($employee['p_id'] == $record['p_id']) {
                echo "found it!";
                unset($employees[$key]);
            }
        }
    }
    
    echo "<pre>";
    print_r($employees);
    

    Outputs

    found it!
    
    Array
    (
        [0] => Array
            (
                [p_id] => T29083999
                [name] => Robert Plaxo
            )
    
        [2] => Array
            (
                [p_id] => T21083911
                [name] => Rhonda Saunders
            )
    
        [3] => Array
            (
                [p_id] => H02910382
                [name] => Miguel Mercado
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题