【问题标题】:How to compare two arrays that contain user details as array and remove the matching elements from one array?如何比较两个包含用户详细信息的数组作为数组并从一个数组中删除匹配的元素?
【发布时间】:2015-12-25 10:34:56
【问题描述】:

我有两个数组 $a$b 在数组中包含“用户详细信息”。

下面是它的结构(即print_r()的输出)

print_r($a);

Array
(
    [0] => Array
    (
            [user_id] => 109
            [profile_page_id] => 0
            [user_name] => profile-109
            [full_name] => Hiten Patel
            [gender] => 0
            [user_image] => 
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png
    )
    [1] => Array
    (
            [user_id] => 1585
            [profile_page_id] => 0
            [user_name] => profile-1585
            [full_name] => Sushil Kadam
            [gender] => 0
            [user_image] => 1585%s.jpg
            [is_invisible] => 0
            [user_group_id] => 7
            [language_id] => 
            [profile_image] => http://app.campusknot.com/file/pic/user/1585_100_square.jpg
    )

    [2] => Array
    (
            [user_id] => 185
            [profile_page_id] => 0
            [user_name] => profile-185
            [full_name] => Perceus Mody
            [gender] => 1
            [user_image] => 185%s.peg
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/file/pic/user/185_100_square.peg
    )

    [3] => Array
    (
            [user_id] => 196
            [profile_page_id] => 0
            [user_name] => profile-196
            [full_name] => Ira Hampton
            [gender] => 1
            [user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png
    )

    [4] => Array
    (
            [user_id] => 244
            [profile_page_id] => 0
            [user_name] => profile-244
            [full_name] => Hiten Patel
            [gender] => 2
            [user_image] => 244%s.cza
            [is_invisible] => 0
            [user_group_id] => 7
            [language_id] => 
            [profile_image] => http://app.campusknot.com/file/pic/user/244_100_square.cza
    ) 
)

第二个数组如下:

print_r($b);


Array
(
    [0] => Array
        (
            [user_id] => 244
            [profile_page_id] => 0
            [user_server_id] => 0
            [user_name] => profile-244
            [full_name] => Hiten Patel
            [gender] => 2
            [user_image] => 244%s.cza
            [is_invisible] => 0
            [user_group_id] => 7
            [language_id] => 
        )

    [1] => Array
        (
            [user_id] => 1585
            [profile_page_id] => 0
            [user_server_id] => 0
            [user_name] => profile-1585
            [full_name] => Sushil Kadam
            [gender] => 0
            [user_image] => 1585%s.jpg
            [is_invisible] => 0
            [user_group_id] => 7
            [language_id] => 
        )

)

现在我想比较两个数组$a and $b,最后我想要精炼的数组$a,它不会包含以下常见的数组元素(数组键应该从0开始并不断增加1)。

print_r($a); //after refinement, the desired output array

Array
(
    [0] => Array
        (
            [user_id] => 109
            [profile_page_id] => 0
            [user_name] => profile-109
            [full_name] => Hiten Patel
            [gender] => 0
            [user_image] => 
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png
        )
    [1] => Array
        (
            [user_id] => 185
            [profile_page_id] => 0
            [user_name] => profile-185
            [full_name] => Perceus Mody
            [gender] => 1
            [user_image] => 185%s.peg
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/file/pic/user/185_100_square.peg
        )
    [2] => Array
        (
            [user_id] => 196
            [profile_page_id] => 0
            [user_name] => profile-196
            [full_name] => Ira Hampton
            [gender] => 1
            [user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
            [is_invisible] => 0
            [user_group_id] => 6
            [language_id] => 
            [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png
        )    
)

为了实现这个期望的输出结果,我尝试了以下代码,但无法获得期望的精确输出结果。

//My attempted code
$a = $result=array_diff($a,$b);

我也尝试了array_diff_assoc() 函数,但这也没有给我想要的输出结果。

有人可以在这方面指导我以带来所需的输出结果吗?

【问题讨论】:

  • Ist megre 他们然后使用 foreach 循环并创建一个新数组,该数组的值对你有用
  • @devpro:如果您可以发布您的代码作为答案,这样我就可以对其进行测试并接受它作为答案会更好。请问你也可以吗?
  • 我已经用唯一值和索引从 0 开始更新了答案
  • 我认为问题已经解决了我的朋友

标签: php arrays multidimensional-array array-difference


【解决方案1】:

返回一个数组,其中包含两个提供的用户列表之间的差异。

1) 给定两个数组:

  • 完整的用户列表 ($userListA)
  • 所需的用户列表 ($userListB)

生成一个数组,其中包含在 $userListA 但不在在 $userListB 中的用户。

working example - eval.in; Source code - pastebin

有一个更高效的版本,在大型数组中会更快。它在user_id 上使用“键控”数组,因此无需扫描所有数组即可检查丢失。 pastebin - compare array for missing user - quick

代码:

/*
 * This version is simple but will be slow for large arrays...
 *
 * Hmm... I may want to do a more efficient version later,  so I will use a
 *        compare function now, so that I can easily change it later.
 */

/*
 * Out Array:
 *    1) In $userListA but not in $userListB
 */
$diffAfromB = array();

// check the input user list...
foreach ($userListA as $userA) {

    // find the userA details in Array B
    if (!matchUserAinB($userA, $userListB)) {
        $diffAfromB[] = $userA;
    }
}

// show otput...
var_dump($diffAfromB);

exit;

// -------------------------------------------------------------------------
/**
 * Check if userA exists in userListB
 *
 * @param array $userA
 * @param array $userListB
 * @return boolean
 */
function matchUserAinB($userA, array $userListB)
{
    $found = false;

    foreach ($userListB as $userB) {
        if ($userB['user_id'] === $userA['user_id']) {
            $found = true;
            break;
        }
    }
    return $found;
}

输出:

array
  0 => 
    array
      'user_id' => string '109' (length=3)
      'profile_page_id' => string '0' (length=1)
      'user_name' => string 'profile-109' (length=11)
      'full_name' => string 'Hiten Patel' (length=11)
      'gender' => string '0' (length=1)
      'user_image' => string '\"\"' (length=4)
      'is_invisible' => string '0' (length=1)
      'user_group_id' => string '6' (length=1)
      'language_id' => string '0' (length=1)
      'profile_image' => string 'http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png' (length=92)
  1 => 
    array
      'user_id' => string '185' (length=3)
      'profile_page_id' => string '0' (length=1)
      'user_name' => string 'profile-185' (length=11)
      'full_name' => string 'Perceus Mody' (length=12)
      'gender' => string '1' (length=1)
      'user_image' => string '185%s.peg' (length=9)
      'is_invisible' => string '0' (length=1)
      'user_group_id' => string '6' (length=1)
      'language_id' => string '0' (length=1)
      'profile_image' => string 'http://app.campusknot.com/file/pic/user/185_100_square.peg' (length=58)
  2 => 
    array
      'user_id' => string '196' (length=3)
      'profile_page_id' => string '0' (length=1)
      'user_name' => string 'profile-196' (length=11)
      'full_name' => string 'Ira Hampton' (length=11)
      'gender' => string '1' (length=1)
      'user_image' => string '2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg' (length=46)
      'is_invisible' => string '0' (length=1)
      'user_group_id' => string '6' (length=1)
      'language_id' => string '0' (length=1)
      'profile_image' => string 'http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png' (length=88)

测试数据:

$sA = 'a:5:{i:0;a:10:{s:7:"user_id";s:3:"109";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-109";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"0";s:10:"user_image";s:4:"\"\"";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:92:"http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png";}i:1;a:10:{s:7:"user_id";s:4:"1585";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:12:"profile-1585";s:9:"full_name";s:12:"Sushil Kadam";s:6:"gender";s:1:"0";s:10:"user_image";s:10:"1585%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";s:13:"profile_image";s:59:"http://app.campusknot.com/file/pic/user/1585_100_square.jpg";}i:2;a:10:{s:7:"user_id";s:3:"185";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-185";s:9:"full_name";s:12:"Perceus Mody";s:6:"gender";s:1:"1";s:10:"user_image";s:9:"185%s.peg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:58:"http://app.campusknot.com/file/pic/user/185_100_square.peg";}i:3;a:10:{s:7:"user_id";s:3:"196";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-196";s:9:"full_name";s:11:"Ira Hampton";s:6:"gender";s:1:"1";s:10:"user_image";s:46:"2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"6";s:11:"language_id";s:1:"0";s:13:"profile_image";s:88:"http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png";}i:4;a:10:{s:7:"user_id";s:3:"244";s:15:"profile_page_id";s:1:"0";s:9:"user_name";s:11:"profile-244";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"2";s:10:"user_image";s:9:"244%s.cza";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";s:13:"profile_image";s:58:"http://app.campusknot.com/file/pic/user/244_100_square.cza";}}';
$userListA = unserialize($sA);

$sB = 'a:2:{i:0;a:10:{s:7:"user_id";s:3:"244";s:15:"profile_page_id";s:1:"0";s:14:"user_server_id";s:1:"0";s:9:"user_name";s:11:"profile-244";s:9:"full_name";s:11:"Hiten Patel";s:6:"gender";s:1:"2";s:10:"user_image";s:9:"244%s.cza";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";}i:1;a:10:{s:7:"user_id";s:4:"1585";s:15:"profile_page_id";s:1:"0";s:14:"user_server_id";s:1:"0";s:9:"user_name";s:12:"profile-1585";s:9:"full_name";s:12:"Sushil Kadam";s:6:"gender";s:1:"0";s:10:"user_image";s:10:"1585%s.jpg";s:12:"is_invisible";s:1:"0";s:13:"user_group_id";s:1:"7";s:11:"language_id";s:1:"0";}}';
$userListB = unserialize($sB);

【讨论】:

    【解决方案2】:

    您可以使用它,但根据您的数组更改索引,这只是一个示例,您如何解决您的问题:

    $a = array(
            array('userid'=>109,
                'profile_img'=>0,
                'username'=>'test',
                'lang'=>0),
            array('userid'=>10,
                'profile_img'=>0,
                'username'=>'test2',
                'lang'=>0),
            array('userid'=>101,
                'profile_img'=>0,
                'username'=>'test',
                'lang'=>0),
            );
    $b = array(
            array('userid'=>100,
                'profile_img'=>0,
                'username'=>'test',
                'lang'=>0),
            array('userid'=>1011,
                'profile_img'=>0,
                'username'=>'test2',
                'lang'=>0),
            array('userid'=>101,
                'profile_img'=>0,
                'username'=>'test',
                'lang'=>0),
            );
    
    
    
    $newArr1 = array();
    
    foreach ($a as $key => $value) {
        $newArr1[$value['userid']] = $value;    
    }
    
    foreach ($b as $key => $value) {
        $newArr2[$value['userid']] = $value;    
    }
    echo "<pre>";
    $diffArr1 = array_diff_key($newArr1, $newArr2);
    $diffArr2 = array_diff_key($newArr2, $newArr1);
    
    $finalArr = array_merge($diffArr1,$diffArr2);
    print_r($finalArr);
    

    结果如下:

    Array
    (
        [0] => Array
            (
                [userid] => 109
                [profile_img] => 0
                [username] => test
                [lang] => 0
            )
    
        [1] => Array
            (
                [userid] => 10
                [profile_img] => 0
                [username] => test2
                [lang] => 0
            )
    
        [2] => Array
            (
                [userid] => 100
                [profile_img] => 0
                [username] => test
                [lang] => 0
            )
    
        [3] => Array
            (
                [userid] => 1011
                [profile_img] => 0
                [username] => test2
                [lang] => 0
            )
    
    )
    

    【讨论】:

    • 首先感谢您的帮助。我想明确一件事,我不希望结果数组中有公共元素。您在答案中添加了 user_id 109 的元素,实际上不应该添加它,而且新数组中的键应该从 0 开始并继续增加 1。请您相应地修改您的答案。我认为你非常接近我实际需要的东西。再次感谢,热切期待您的回复。
    • 好吧,您正在寻找重新排列数组答案已更新@user2839497
    • 公共数组元素也不应该在结果数组中重复。
    • @user2839497:答案再次更新为唯一值
    • 你在array_merge之后没有在你的代码中使用$c?
    【解决方案3】:

    首先我给$output $a 以获取$a 中的所有元素

    之后我将它与$b 进行比较并删除$output 中的存在值

        $output = $a;
        foreach ($b as $k => $v) {
            $exists = false;
            foreach($output as $key => $value) {
                if ($v == $value) {
                    unset($output[$key]);
                    $exists = true;
                    break;
                } else {
                    continue;
                }
            } 
            if (!$exists) {
                $output[] = $v;
            }
        }
        echo '<pre>';
        print_r($output);
        echo '</pre>';
    

    结果

        Array
        (
            [0] => Array
                (
                    [user_id] => 109
                    [profile_page_id] => 0
                    [user_name] => profile-109
                    [full_name] => Hiten Patel
                    [gender] => 0
                    [user_image] => 
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                    [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile_100.png
                )
            [1] => Array
                (
                    [user_id] => 185
                    [profile_page_id] => 0
                    [user_name] => profile-185
                    [full_name] => Perceus Mody
                    [gender] => 1
                    [user_image] => 185%s.peg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                    [profile_image] => http://app.campusknot.com/file/pic/user/185_100_square.peg
                )
            [2] => Array
                (
                    [user_id] => 196
                    [profile_page_id] => 0
                    [user_name] => profile-196
                    [full_name] => Ira Hampton
                    [gender] => 1
                    [user_image] => 2014/11/24c4a6835e667b67b82cea3666841ac7%s.jpg
                    [is_invisible] => 0
                    [user_group_id] => 6
                    [language_id] => 
                    [profile_image] => http://app.campusknot.com/theme/frontend/foxplus/style/default/image/noimage/profile.png
                )    
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多