PHP多维数组 - 删除重复 [英] PHP Multidimensional Arrays - Remove Duplicates

查看:133
本文介绍了PHP多维数组 - 删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果任何人都可以请帮我在这里,我将永远感激,因为我已经花了约2整天现在正试图得到这个工作。我想取两多维数组并加以比较,那么的删除的任何重复的记录。

if anyone could please help me here I would be eternally grateful as I've spent about 2 full days now trying to get this to work. I want to take two multidimensional arrays and compare them, then remove any duplicate records.

的场景是:在数组2中的值已被分配给一个用户的配置文件。在数组1的值是ALL,用户可以选择的可用值。我想在两个比较,以便只有尚未分配给出作为一个选项(左阵列中)...

The scenario is: The values in array2 have already been assigned to a user's profile. The values in array1 are ALL of the available values that the user can choose from. I want to compare the two so that only the ones not already assigned are given as an option (left in the array)...

$array1 = array(
  [0] => array( [id] => 3 [name] => Eye Colour )
  [1] => array( [id] => 1 [name] => Hair Colour )
  [2] => array( [id] => 5 [name] => Hair Length )
  [3] => array( [id] => 4 [name] => Height )
); 

$array2 = array(
  [0] => array( [attribute_id] => 3 [name] => Eye Colour [active] => 1 )
  [1] => array( [attribute_id] => 5 [name] => Hair Length [active] => 1 ) )
);

PHP的和array_diff()函数不与多维数组的工作,而且我身边有一个很好的搜索,但似乎无法找到任何对我的作品!

PHP's array_diff() function doesn't work with multidimensional arrays, and I've had a good search around but can't seem to find anything that works for me!

基于上述两个阵列的结果应该是:

The result based on the above two arrays should be:

$array1 = array(
  [0] => array( [id] => 1 [name] => Hair Colour )
  [1] => array( [id] => 4 [name] => Height )
);

在[活动]字段是无关的,所以我只需要它来比较ID和Name字段。我意识到,这两个ID字段的名称不同,但是这将是一个痛苦的改变它们,因为它们的数据库列名。

The [active] field is irrelevant, so I just need it to compare the ID and the Name fields. I realise that the name of the two id fields is different, but it would be a pain to change them as they are database column names.

它需要完全除去阵列,而不仅仅是值。我已经受够了previous尝试然后离开那里阵列(),然后这会导致问题,当我循环遍历数组生成,用户可以选择的领域的问题。

It needs to completely remove the array, not just the values. I've had issues with previous attempts where it leaves array( ) in there and then this causes issues when I'm looping through the array generating the fields that the user can choose from.

请帮忙。我会买你很多啤酒! :)

Please help. I will buy you many beers! :)

谢谢,
史蒂夫

Thanks, Steve

推荐答案

我不知道如何做到这一点任何内置的PHP函数,但这里有一个自定义的:

I don't know how to do it with any built-in PHP function but here's a custom one:

$array1 = array(
  array( 'id' => 3, 'name' => 'Eye Colour' ),
  array( 'id' => 1, 'name' => 'Hair Colour' ),
  array( 'id' => 5, 'name' => 'Hair Length' ),
  array( 'id' => 4, 'name' => 'Height' ),
); 

$array2 = array(
  array( 'attribute_id' => 3, 'name' => 'Eye Colour', 'active' => 1 ),
  array( 'attribute_id' => 5, 'name' => 'Hair Length', 'active' => 1 )
);

// function to remove duplicates
function myArrayDiff($array1, $array2) {
    // loop through each item on the first array
    foreach ($array1 as $key => $row) {
        // loop through array 2 and compare
        foreach ($array2 as $key2 => $row2) {
            if ($row['id'] == $row2['attribute_id']) {
                // if we found a match unset and break out of the loop
                unset($array1[$key]);
                break;
            }
        }
    }

    return array_values($array1);
}

$array3 = myArrayDiff($array1, $array2);

print_r($array3);

/* result:
    Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Hair Colour
            )

        [1] => Array
            (
                [id] => 4
                [name] => Height
            )

    )
*/

这篇关于PHP多维数组 - 删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆