dongping2023
2014-04-19 15:33
浏览 53
已采纳

PHP多维数组删除部分重复项

I have an array containing this

Array ( 
[0] => Array ( [0] => Sub1 ) 
[1] => Array ( [0] => Sub1 [1] => Gold ) 
[2] => Array ( [0] => Sub1 [1] => Sub2 ) 
[3] => Array ( [0] => Sub1 [1] => Sub2 [2] => Sub3 ) 
[4] => Array ( [0] => Sub1 [1] => Sub2 [2] => Sub3 [3] => Sub4 ) 
[5] => Array ( [0] => Sub1 [1] => Test ) 
)

How can i remove the array [2] [3] as they are existent in [4]

and the array would be like

Array (  
[0] => Array ( [0] => Sub1 [1] => Gold )  
[1] => Array ( [0] => Sub1 [1] => Sub2 [2] => Sub3 [3] => Sub4 ) 
[2] => Array ( [0] => Sub1 [1] => Test ) 
)

Is it possible to automate?

图片转代码服务由CSDN问答提供 功能建议

我有一个包含此

 数组的数组(
  [0] =>数组([0] => Sub1)
 [1] =>数组([0] => Sub1 [1] => Gold)
 [2] =>数组 ([0] => Sub1 [1] => Sub2)
 [3] =>数组([0] => Sub1 [1] => Sub2 [2] => Sub3)
  [4] =>数组([0] => Sub1 [1] => Sub2 [2] => Sub3 [3] => Sub4)
 [5] =>数组([0]  => Sub1 [1] =>测试)
)
   
 
 

如何删除数组[2] [3],因为它们在[ 4]

数组就像

 数组(
 [0] =>数组([0] =>  ; Sub1 [1] => Gold)
 [1] =>数组([0] => Sub1 [1] => Sub2 [2] => Sub3 [3] => Sub4)\  n [2] =>数组([0] => Sub1 [1] =>测试)
)
   
 
 

是否可以自动化?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtm41506 2014-04-19 15:43
    已采纳
    $result = array();
    foreach ($original as $orig_el) {
        $found = false;
        foreach ($result as &$new_el) {
            if (count($orig_el) >= count($new_el) && array_slice($orig_el, 0, count($new_el)) == $new_el) {
                $new_el = $orig_el;
                $found = true;
                break;
            }
        }
        if (!$found) {
            $result[] = $orig_el;
        }
    }
    
    点赞 评论

相关推荐 更多相似问题