检测一个循环数组中的子集项

Posted

技术标签:

【中文标题】检测一个循环数组中的子集项【英文标题】:Detect subset item in One looping array 【发布时间】:2019-09-03 03:27:31 【问题描述】:

我有一些任务要检测是否有任何项目是来自下一个循环一个数组的下一个项目的子集,这是为了从该数组中取消设置该项目的子集,该项目是该数组中下一个项目的子集。

这是我的数组来自循环:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Sabrino
                    [duration] => 7
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

        )

    [2] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Building Relation Query
                    [duration] => 3
                )

        )

    [3] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Building Relation Query
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => Doing Looping
                    [duration] => 1
                )

        )

    [4] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Building Relation Query
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => Introduction To Laravel
                    [duration] => 1
                )

        )

    [5] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Building Relation Query
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => Learn Function
                    [duration] => 1
                )

        )

    [6] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Building Relation Query
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => improve something
                    [duration] => 1
                )

        )

    [7] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Make View Controller and Index
                    [duration] => 3
                )

        )

    [8] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Make View Controller and Index
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => Doing Looping
                    [duration] => 1
                )

        )

    [9] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Pr pr pr
                    [duration] => 3
                )

            [1] => stdClass Object
                (
                    [name] => Make View Controller and Index
                    [duration] => 3
                )

            [2] => stdClass Object
                (
                    [name] => Introduction To Laravel
                    [duration] => 1
                )

        )

)

我使用此代码来检测子集:

for ($i = 0; $i < count($sugesti); ++$i) 
    if (!array_diff($sugesti[$i], $sugesti[$i + 1])) 
        unset($sugesti[$i]);
    

我想从下一个数组中删除作为子集的数组

【问题讨论】:

很难跟上您正在尝试做的事情。也许如果您提供上面输出的示例,我们可以更好地理解您在寻找什么, 我想显示该数组中唯一的唯一项 【参考方案1】:

试试这样的:

$existing = [];

foreach ($sugesti as $key => $sugest) 
    foreach ($sugest as $key2 => $sug 
        if (in_array($sug->name, $existing)) 
            unset($sugesti[$key][$key2];
            continue;
        
        $existing[] = $sug->name;
    

【讨论】:

in_array 真的是这样吗? 很好地拿起。对不起,我不得不去做一些事情,然后在中途回来。我已经更新了示例。 我想要的结果仍然是错误的,如果我使用那个sintaks,它会给出一个空数组,但是如果在unset函数中删除key2,它也会删除第一个不重复的唯一元素显示在数组中,但数组中仍有重复项 @sab896 很抱歉,您需要更清楚自己想要什么或调整代码以满足您的需求。

以上是关于检测一个循环数组中的子集项的主要内容,如果未能解决你的问题,请参考以下文章

删除数组重复项

js数组方法 - 检测两个数组是否有重复项

循环遍历对象数组,并将它们转换为 UL 中的 LI 项 [重复]

想要使用循环将数组列表项放入变量中

数据结构和算法爆肝三万字你必须知道的20个解决问题的技巧

数据结构和算法爆肝三万字你必须知道的20个解决问题的技巧