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

哈西特·塞西(Harshit Sethi)

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

$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/>";
             }
         }
李·巴利诺

找到具有重复的索引后,可以取消设置这些值。因为如果在检查重复项的for循环中取消设置它,则将产生此错误,因为数组的大小/计数更改并且索引不再匹配:

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

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

$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);

输出:

Before:7
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128@canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321@canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [2] => Array ( [customer_name] => pppoo [profile_image] => http://smartpetmanagement.com/upload/35210.png [jid] => yyy329@canopus-pc [customer_id] => 329 [phone] => 9525538835 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330@canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => 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 ) [5] => Array ( [customer_name] => Pet [profile_image ] => [jid] => pet590@canopus-pc [customer_id] => 590 [phone] => 6560530537 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599@canopus-pc [customer_id] => 599 [phone] => 2828282822 ) ) 

Match INDEX on result 0 == customerlist 2
Match INDEX on result 1 == customerlist 5

After: 5
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128@canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321@canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330@canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => 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 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599@canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章