根据键的重复值从数组中删除行

时间:2014-04-26 14:12:26

标签: php

我有一个名为$data['job_list']的数组,如下所示

Array
(
    [0] => Array
        (
            [job_id] => 2
            [job_title] => JQuery developer
            [job_desc] => Developer
            [job_slug] => 2-JQuery-developer
            [job_type] => 191
            [job_skill] => 2
            [job_salary] => 2
            [job_experience] => 1
            [company_name] => IGUTS
            [company_desc] => IGUTS is a fresh company
            [company_industry] => 24
            [company_address] => 35 Lawrence Street
            [company_state] => 35
            [company_city] => 650
            [user_id] => 1
            [concerned_fname] => Saswat
            [concerned_lname] => Routroy
            [contact] => 8961287928
            [date_of_post] => 26-04-2014
            [job_timestamp] => 1398517810
            [industry_id] => 191
            [industry_title] => Web Designer/Developer
            [p_cid] => 24
            [industry_slug] => 191-Web-Designer-Developer
            [industry_desc] => 
            [industry_image] => 
            [industry_priority] => 0
            [industry_timestamp] => 1396535046
        )

    [1] => Array
        (
            [job_id] => 1
            [job_title] => PHP developer
            [job_desc] => Developer
            [job_slug] => 1-PHP-developer
            [job_type] => 191
            [job_skill] => 1,2
            [job_salary] => 1
            [job_experience] => 1
            [company_name] => IGUTS
            [company_desc] => IGUTS Company
            [company_industry] => 24
            [company_address] => 35 Lawrence Street
            [company_state] => 35
            [company_city] => 650
            [user_id] => 1
            [concerned_fname] => Saswat
            [concerned_lname] => Routroy
            [contact] => 8961287928
            [date_of_post] => 18-04-2014
            [job_timestamp] => 1397842605
            [skill_id] => 2
            [skill_title] => JQuery
            [skill_slug] => 2-JQuery
            [industry] => 24
            [skill_timestamp] => 1397395987
        )

    [2] => Array
        (
            [job_id] => 2
            [job_title] => JQuery developer
            [job_desc] => Developer
            [job_slug] => 2-JQuery-developer
            [job_type] => 191
            [job_skill] => 2
            [job_salary] => 2
            [job_experience] => 1
            [company_name] => IGUTS
            [company_desc] => IGUTS is a fresh company
            [company_industry] => 24
            [company_address] => 35 Lawrence Street
            [company_state] => 35
            [company_city] => 650
            [user_id] => 1
            [concerned_fname] => Saswat
            [concerned_lname] => Routroy
            [contact] => 8961287928
            [date_of_post] => 26-04-2014
            [job_timestamp] => 1398517810
            [skill_id] => 2
            [skill_title] => JQuery
            [skill_slug] => 2-JQuery
            [industry] => 24
            [skill_timestamp] => 1397395987
        )

)

[job_id] => 2出现两次

我想要的是,应删除带有重复job_id => 2的行

我怎样才能实现?

3 个答案:

答案 0 :(得分:2)

$jobIds = array();

foreach ($jobs as $key => $job) {
    if (in_array($jobIds, $job['job_id'])) {
        unset($jobs[$key]);
        continue;
    }
    $jobIds[] = $job['job_id'];
}

print_r($jobs);

答案 1 :(得分:0)

我会循环完成每个工作#34;并创建一个作业ID数组。如果我看到相同的ID两次,那么我会删除副本,并在需要时将其存储在其他地方。

答案 2 :(得分:0)

正如Matei Mihai所说,我对他的剧本和宾果游戏做了一些小改动,但它已经解决了。

Matei Mihai在in_array()函数的参数的数据类型上犯了一个错误

我解决了这个问题。

这是我的代码

    $jobIds = array();

    for($i = 0;$i < count($data['job_list']); $i++ )
    {
        if (in_array($data['job_list'][$i]['job_id'], $jobIds))
            {
                unset($data['job_list'][$i]);
                continue;
            }
        $jobIds[] = $data['job_list'][$i]['job_id'];
    }