gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:00:24 24 4
gpt4 key购买 nike

我有一个包含这个的数组

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 )
)

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

数组就像

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

是否可以自动化?

最佳答案

$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;
}
}

关于PHP多维数组删除部分重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23171938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com