从多维数组中按嵌套数组键移除重复的嵌套数组。

huangapple go评论53阅读模式
英文:

Remove Duplicate Nested Arrays From Multidimenssional Array By Nested Array Key

问题

我试图创建一个数组,在这个数组中,我需要使用特定的键(fileid)进行过滤,以便只返回父数组的唯一键。以下是我的代码:

Array
(
    [ABCD] => Array
        (
            [0] => Array
                (
                    [fileid] => 5454554
                    [filename] => myfile1.txt
                )

            [1] => Array
                (
                    [fileid] => 5454954
                    [filename] => myfile2.txt
                )

            [2] => Array
                (
                    [fileid] => 5454554
                    [filename] => myfile1.txt
                )
        )
    
    [EFGH] => Array
        (
            [0] => Array
                (
                    [fileid] => 5429654
                    [filename] => myfile2.txt
                )

            [1] => Array
                (
                    [fileid] => 5433954
                    [filename] => myfile2.txt
                )

            [2] => Array
                (
                    [fileid] => 5429654
                    [filename] => myfile1.txt
                )
        )
)

第一层键(ABCDEFGH)是文件所有者的ID。嵌套的数组包含文件ID和文件名。我需要对它进行过滤,以删除每个重复的嵌套数组(具有相同的fileid)。

我尝试了以下方法链接1链接2,以及在网上找到的许多其他解决方案。但都没有成功。我还尝试了以下代码:

$result =  array_map("unserialize", array_unique(array_map("serialize", $file_array)));

但仍然没有成功。请问有人可以指导我走向正确的方向吗?

先感谢您。

英文:

I am trying to create an array where I need to filter with a certain key (fileid) so that it returns only unique keys for parent array. Here is what I have..

Array
(
	[ABCD] => Array
		(
			[0] => Array
				(
					[fileid] => 5454554
					[filename] => myfile1.txt
				)

			[1] => Array
				(
					[fileid] => 5454954
					[filename] => myfile2.txt
				)

			[2] => Array
				(
					[fileid] => 5454554
					[filename] => myfile1.txt
				)

		)

	[EFGH] => Array
		(
			[0] => Array
				(
					[fileid] => 5429654
					[filename] => myfile2.txt
				)

			[1] => Array
				(
					[fileid] => 5433954
					[filename] => myfile2.txt
				)

			[2] => Array
				(
					[fileid] => 5429654
					[filename] => myfile1.txt
				)

		)

)

The first keys (ABCD and EFGH) are File Owner IDs. The nested arrays contains the File ID and the FIle Name. I need to filter it so that every duplicate nested array (having same fileid) is removed.

I tried this this and this and many other solutions found on the web. But no luck. I also tried

$result =  array_map("unserialize", array_unique(array_map("serialize", $file_array)));  

No Luck again. Can someone please guide me to the right direction?

Thanks in advance.

答案1

得分: 2

以下是翻译好的部分:

A simple couple of loops where you remember the fileid's that you have seen in the inner array. If you see a fileid twice, you unset that occurrence of the array.
Note using &$owners in the outer loop so you can actually remove an occurrence from within the foreach loop

一个简单的循环,其中您记住了内部数组中已经看到的fileid。如果您看到一个fileid两次,就取消设置数组中的那个出现。请注意,在外部循环中使用&$owners,这样您可以实际上从foreach循环内部删除一个出现。

RESULT

结果

OR

或者

Build a new array containing only the non-duplicated files

构建一个只包含不重复文件的新数组

代码部分未翻译。

英文:

A simple couple of loops where you remember the fileid's that you have seen in the inner array. If you see a fileid twice, you unset that occurance of the array.
Note using &$owners in the outer loop so you can actually remove an occurance from within the foreach loop

$input = [  'ABCD' => [
                    ['fileid' => 5454554, 'filename' => 'myfile1.txt'],
                    ['fileid' => 5454954, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5454554, 'filename' => 'myfile1.txt']
                    ],
            'EFGH' => [
                    ['fileid' => 5429654, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5433954, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5429654, 'filename' => 'myfile1.txt']
            ]
];
foreach ($input as &$owners){
    $ids = [];
    foreach($owners as $i=>$file){
        if ( in_array($file['fileid'], $ids) ){
            unset($owners[$i]);
        } else {
            $ids[] = $file['fileid'];
        }
    }
}
print_r($input);

RESULT

Array
(
    [ABCD] => Array
        (
            [0] => Array
                ( [fileid] => 5454554, [filename] => myfile1.txt )

            [1] => Array
                ( [fileid] => 5454954, [filename] => myfile2.txt )
        )
    [EFGH] => Array
        (
            [0] => Array
                ( [fileid] => 5429654, [filename] => myfile2.txt )

            [1] => Array
                ( [fileid] => 5433954,  [filename] => myfile2.txt )
        )
)

OR

Build a new array containing only the non duplicated files

$input = [  'ABCD' => [
                    ['fileid' => 5454554, 'filename' => 'myfile1.txt'],
                    ['fileid' => 5454954, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5454554, 'filename' => 'myfile1.txt']
                    ],
            'EFGH' => [
                    ['fileid' => 5429654, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5433954, 'filename' => 'myfile2.txt'],
                    ['fileid' => 5429654, 'filename' => 'myfile1.txt']
            ]
];
$new = [];
foreach ($input as $owner => $files){
    $t = [];
    $ids = [];

    foreach($files as $i=>$file){
        if ( ! in_array($file['fileid'], $ids) ){
            $t[] = $file;
            $ids[] = $file['fileid'];
        }
    }
    $new[$owner] = $t;
}
print_r($new);

答案2

得分: 1

你可以使用array_maparray_filter的组合,如下所示:

array_map(
    function($arr) {
        $exists = [];
        return array_values(
            array_filter(
                $arr,
                function($arr1) use (&$exists) {
                    if (isset($exists[$arr1['fileid']])) {
                        return false;
                    }
                    $exists[$arr1['fileid']] = true;
                    return true;
                }
            )
        );
    },
    $file_array
);

注意:我使用了array_values来确保结果数组具有连续的数值键。如果你想保留原始的数值键,可以省略它。

英文:

You can use a combination of array_map and array_filter like so:

array_map(
	function($arr) {
		$exists = [];
		return array_values(
			array_filter(
				$arr,
				function($arr1) use (&$exists) {
					if (isset($exists[$arr1['fileid']])) {
						return false;
					}
					$exists[$arr1['fileid']] = true;
					return true;
				}
			)
		);
	},
	$file_array,
);

Note: I used array_values to ensure the resultant array has consecutive numerical keys. If you want to keep the original numerical keys then it can be omitted.

huangapple
  • 本文由 发表于 2023年3月3日 22:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定