从php中的两个数组中删除重复项

时间:2016-03-08 05:47:37

标签: php arrays

我一直坚持以下删除数组重复的问题

$result=array(2) { [0]=> object(stdClass)#489 (5) { ["id"]=> string(2) "64" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "329" ["block_status"]=> string(1) "0" ["username"]=> string(5) "pppoo" } [1]=> object(stdClass)#490 (5) { ["id"]=> string(2) "65" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "590" ["block_status"]=> string(1) "0" ["username"]=> string(3) "Pet" } } 

$customerlist= array(7) { [0]=> object(stdClass)#491 (5) { ["customer_name"]=> string(4) "User" ["profile_image"]=> string(47) "http://pet.huarisnaque.com/pet/upload/90113.png" ["jid"]=> string(18) "user128@canopus-pc" ["customer_id"]=> string(3) "128" ["phone"]=> string(10) "4784784784" } [1]=> object(stdClass)#494 (5) { ["customer_name"]=> string(6) "Khatru" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/61694.png" ["jid"]=> string(20) "khatru321@canopus-pc" ["customer_id"]=> string(3) "321" ["phone"]=> string(10) "9686838386" } [2]=> object(stdClass)#495 (5) { ["customer_name"]=> string(5) "pppoo" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/35210.png" ["jid"]=> string(17) "yyy329@canopus-pc" ["customer_id"]=> string(3) "329" ["phone"]=> string(10) "9525538835" } [3]=> object(stdClass)#496 (5) { ["customer_name"]=> string(7) "Xitxitx" ["profile_image"]=> NULL ["jid"]=> string(21) "xitxitx330@canopus-pc" ["customer_id"]=> string(3) "330" ["phone"]=> string(10) "6535383535" } [4]=> object(stdClass)#497 (5) { ["customer_name"]=> string(25) "The Following Document yf" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/13712.png" ["jid"]=> string(39) "the following document yf589@canopus-pc" ["customer_id"]=> string(3) "589" ["phone"]=> string(10) "9535383535" } [5]=> object(stdClass)#498 (5) { ["customer_name"]=> string(3) "Pet" ["profile_image"]=> NULL ["jid"]=> string(17) "pet590@canopus-pc" ["customer_id"]=> string(3) "590" ["phone"]=> string(10) "6560530537" } [6]=> object(stdClass)#499 (5) { ["customer_name"]=> string(10) "Sanjay Pra" ["profile_image"]=> NULL ["jid"]=> string(24) "sanjay pra599@canopus-pc" ["customer_id"]=> string(3) "599" ["phone"]=> string(10) "2828282822" } } 

有两个数组,我们需要从包含来自customerlist的元素的结果中包含两个元素的数组中删除重复记录。 这是我的方法

for($i=0;$i<count($customerslist);$i++)
         {
             for($j=0;$j<count($result);$i++)
             {
                // if($result[$j]->block_to_id==$customerslist[$i]->customer_id)
                 {
                     unset($customerslist[$i]);
                 }
                     echo $result[$j]->block_to_id."<br/>";
             }
         }

1 个答案:

答案 0 :(得分:1)

您可以在找到具有重复项的索引后取消设置值。因为如果你在检查你的重复项的for循环中取消它会产生这个错误,因为数组的大小/数量改变而且索引不再匹配:

  

注意:未定义的偏移量:2

所以我的解决方案是你可以将匹配的索引放在另一个数组上,然后在另一个for循环中取消设置它们:

private ArrayList<addValuesContact> GetSearchResults() {
    ArrayList<addValuesContact> results = new ArrayList<>();

    for (int i=0;i<6;i++) {
        addValuesContact sr1 = new addValuesContact();
        sr1.setImage(icons[i]);
        sr1.setTv1(desc[i]);
        sr1.setDetails(details[i]);
        results.add(sr1);
    }

    return results;
}

<强>输出:

$result=array(
array('id' => 64, 
    "block_from_id" => 117,
    "block_to_id" => 329,
    "block_status" => 0,
    "username" => "pppoo"),
array("id"=> 65,
    "block_from_id"=> 117,
    "block_to_id"=> 590,
    "block_status"=> 0,
    "username"=> "Pet"
    )
);

$customerlist= array(
array("customer_name" => "User" ,"profile_image" => "http://pet.huarisnaque.com/pet/upload/90113.png" ,"jid" => "user128@canopus-pc" ,"customer_id" => "128" ,"phone" => "4784784784"),
array("customer_name" => "Khatru" ,"profile_image" => "http://smartpetmanagement.com/upload/61694.png" ,"jid" => "khatru321@canopus-pc" ,"customer_id" => "321" ,"phone" => "9686838386"),
array("customer_name" => "pppoo" ,"profile_image" => "http://smartpetmanagement.com/upload/35210.png" ,"jid" => "yyy329@canopus-pc" ,"customer_id" => "329" ,"phone" => "9525538835"),
array("customer_name" => "Xitxitx" ,"profile_image " => NULL ,"jid" => "xitxitx330@canopus-pc" ,"customer_id" => "330" ,"phone" => "6535383535"),
array("customer_name" => "The Following Document yf" ,"profile_image" => "http://smartpetmanagement.com/upload/13712.png" ,"jid" => "the following document yf589@canopus-pc" ,"customer_id" => "589" ,"phone" => "9535383535"),
array("customer_name" => "Pet" ,"profile_image " => NULL ,"jid" => "pet590@canopus-pc" ,"customer_id" => "590" ,"phone" => "6560530537"),
array("customer_name" => "Sanjay Pra" ,"profile_image " => NULL ,"jid" => "sanjay pra599@canopus-pc" ,"customer_id" => "599" ,"phone" => "2828282822")
);
echo "Before:". sizeof($customerlist) ."<br>";
print_r($customerlist);
echo "<br>";
echo "<br>";
$match = array();

for ($i=0; $i < sizeof($result) ; $i++) { 
    for ($ii=0; $ii < sizeof($customerlist) ; $ii++) { 
        if ($result[$i]['block_to_id'] == $customerlist[$ii]['customer_id']) {
            $match[] = $ii;
            echo " Match INDEX on result $i == customerlist $ii<br>";
        }
    }
}
for ($i=0; $i < sizeof($match) ; $i++) { 
    $ii = $match[$i];
    unset($customerlist[$ii]);
}
echo "<br>";

echo "After: ". sizeof($customerlist) ."<br>";
print_r($customerlist);