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

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

Remove Duplicate Nested Arrays From Multidimenssional Array By Nested Array Key

问题

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

  1. Array
  2. (
  3. [ABCD] => Array
  4. (
  5. [0] => Array
  6. (
  7. [fileid] => 5454554
  8. [filename] => myfile1.txt
  9. )
  10. [1] => Array
  11. (
  12. [fileid] => 5454954
  13. [filename] => myfile2.txt
  14. )
  15. [2] => Array
  16. (
  17. [fileid] => 5454554
  18. [filename] => myfile1.txt
  19. )
  20. )
  21. [EFGH] => Array
  22. (
  23. [0] => Array
  24. (
  25. [fileid] => 5429654
  26. [filename] => myfile2.txt
  27. )
  28. [1] => Array
  29. (
  30. [fileid] => 5433954
  31. [filename] => myfile2.txt
  32. )
  33. [2] => Array
  34. (
  35. [fileid] => 5429654
  36. [filename] => myfile1.txt
  37. )
  38. )
  39. )

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

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

  1. $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..

  1. Array
  2. (
  3. [ABCD] => Array
  4. (
  5. [0] => Array
  6. (
  7. [fileid] => 5454554
  8. [filename] => myfile1.txt
  9. )
  10. [1] => Array
  11. (
  12. [fileid] => 5454954
  13. [filename] => myfile2.txt
  14. )
  15. [2] => Array
  16. (
  17. [fileid] => 5454554
  18. [filename] => myfile1.txt
  19. )
  20. )
  21. [EFGH] => Array
  22. (
  23. [0] => Array
  24. (
  25. [fileid] => 5429654
  26. [filename] => myfile2.txt
  27. )
  28. [1] => Array
  29. (
  30. [fileid] => 5433954
  31. [filename] => myfile2.txt
  32. )
  33. [2] => Array
  34. (
  35. [fileid] => 5429654
  36. [filename] => myfile1.txt
  37. )
  38. )
  39. )

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

  1. $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

  1. $input = [ 'ABCD' => [
  2. ['fileid' => 5454554, 'filename' => 'myfile1.txt'],
  3. ['fileid' => 5454954, 'filename' => 'myfile2.txt'],
  4. ['fileid' => 5454554, 'filename' => 'myfile1.txt']
  5. ],
  6. 'EFGH' => [
  7. ['fileid' => 5429654, 'filename' => 'myfile2.txt'],
  8. ['fileid' => 5433954, 'filename' => 'myfile2.txt'],
  9. ['fileid' => 5429654, 'filename' => 'myfile1.txt']
  10. ]
  11. ];
  12. foreach ($input as &$owners){
  13. $ids = [];
  14. foreach($owners as $i=>$file){
  15. if ( in_array($file['fileid'], $ids) ){
  16. unset($owners[$i]);
  17. } else {
  18. $ids[] = $file['fileid'];
  19. }
  20. }
  21. }
  22. print_r($input);

RESULT

  1. Array
  2. (
  3. [ABCD] => Array
  4. (
  5. [0] => Array
  6. ( [fileid] => 5454554, [filename] => myfile1.txt )
  7. [1] => Array
  8. ( [fileid] => 5454954, [filename] => myfile2.txt )
  9. )
  10. [EFGH] => Array
  11. (
  12. [0] => Array
  13. ( [fileid] => 5429654, [filename] => myfile2.txt )
  14. [1] => Array
  15. ( [fileid] => 5433954, [filename] => myfile2.txt )
  16. )
  17. )

OR

Build a new array containing only the non duplicated files

  1. $input = [ 'ABCD' => [
  2. ['fileid' => 5454554, 'filename' => 'myfile1.txt'],
  3. ['fileid' => 5454954, 'filename' => 'myfile2.txt'],
  4. ['fileid' => 5454554, 'filename' => 'myfile1.txt']
  5. ],
  6. 'EFGH' => [
  7. ['fileid' => 5429654, 'filename' => 'myfile2.txt'],
  8. ['fileid' => 5433954, 'filename' => 'myfile2.txt'],
  9. ['fileid' => 5429654, 'filename' => 'myfile1.txt']
  10. ]
  11. ];
  12. $new = [];
  13. foreach ($input as $owner => $files){
  14. $t = [];
  15. $ids = [];
  16. foreach($files as $i=>$file){
  17. if ( ! in_array($file['fileid'], $ids) ){
  18. $t[] = $file;
  19. $ids[] = $file['fileid'];
  20. }
  21. }
  22. $new[$owner] = $t;
  23. }
  24. print_r($new);

答案2

得分: 1

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

  1. array_map(
  2. function($arr) {
  3. $exists = [];
  4. return array_values(
  5. array_filter(
  6. $arr,
  7. function($arr1) use (&$exists) {
  8. if (isset($exists[$arr1['fileid']])) {
  9. return false;
  10. }
  11. $exists[$arr1['fileid']] = true;
  12. return true;
  13. }
  14. )
  15. );
  16. },
  17. $file_array
  18. );

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

英文:

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

  1. array_map(
  2. function($arr) {
  3. $exists = [];
  4. return array_values(
  5. array_filter(
  6. $arr,
  7. function($arr1) use (&$exists) {
  8. if (isset($exists[$arr1['fileid']])) {
  9. return false;
  10. }
  11. $exists[$arr1['fileid']] = true;
  12. return true;
  13. }
  14. )
  15. );
  16. },
  17. $file_array,
  18. );

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:

确定