Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中为特定键从多维数组中删除重复值_Php_Arrays_Multidimensional Array_Remove - Fatal编程技术网

如何在php中为特定键从多维数组中删除重复值

如何在php中为特定键从多维数组中删除重复值,php,arrays,multidimensional-array,remove,Php,Arrays,Multidimensional Array,Remove,我在这里搜索解决方案,但没有找到适合我的用例的解决方案 我有一个大的数组,它是这样构建的: Array ( [0] => Array ( [Template] => page.html5 ) [1] => Array ( [Template] => page2.html5 ) [2] => Array (

我在这里搜索解决方案,但没有找到适合我的用例的解决方案

我有一个大的
数组
,它是这样构建的:

Array
(
    [0] => Array
        (
            [Template] => page.html5
        )

    [1] => Array
        (
            [Template] => page2.html5
        )

    [2] => Array
        (
            [Template] => page.html5
        )

    [3] => Array
        (
            [Template] => page2.html5
        )

    [4] => Array
        (
            [Template] => page.html5
        )

    [5] => Array
        (
            [Template] => page2.html5
        )

    [6] => Array
        (
            [id] => 27
            [table] => tl_custom
            [type] => text
            [data] => Array
                (
                    [fragment] => example
                    [previewId] => 1
                    [isActive] => 1
                )

        )

)
我想删除
数组
键“模板”的所有重复值,但除此之外,我希望
数组
保持原样

因此,之后我的数组应该如下所示:

Array
(
    [0] => Array
        (
            [Template] => page.html5
        )

    [1] => Array
        (
            [Template] => page2.html5
        )
    [6] => Array
        (
            [id] => 27
            [table] => tl_custom
            [type] => text
            [data] => Array
                (
                    [fragment] => example
                    [previewId] => 1
                    [isActive] => 1
                )

        )

)
有没有一种方法可以在不使用大量内存的情况下实现这一点?
感谢您的回答:)

下面的代码循环数组,标记要删除的索引,然后另一个循环执行删除操作:

$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
    if (isset($input[$index]["Template"])) { //Ignore items where there is no template
        if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
            $templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
        } else { //Mark for removal
            $templates[$input[$index]["Template"]][]=$index;
        }
    }
}

//Actual removals
foreach($templates => $index) {
    //Removing the actual element:
    unset($input[$index]["Template"]);
    //Remove the parent as well if it becomes empty
    if (!count($input[$index])) unset($input[$index]);
}
$templates=array()//这将存储删除计划
对于($index=0;$index$index){
//删除实际元素:
取消设置($input[$index][“模板]);
//如果父级变为空,也将其删除
如果(!count($input[$index])未设置($input[$index]);
}
此算法的内存需求为:


平均值(元素大小)*元素数

下面的代码循环数组,标记要删除的索引,然后另一个循环执行删除操作:

$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
    if (isset($input[$index]["Template"])) { //Ignore items where there is no template
        if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
            $templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
        } else { //Mark for removal
            $templates[$input[$index]["Template"]][]=$index;
        }
    }
}

//Actual removals
foreach($templates => $index) {
    //Removing the actual element:
    unset($input[$index]["Template"]);
    //Remove the parent as well if it becomes empty
    if (!count($input[$index])) unset($input[$index]);
}
$templates=array()//这将存储删除计划
对于($index=0;$index$index){
//删除实际元素:
取消设置($input[$index][“模板]);
//如果父级变为空,也将其删除
如果(!count($input[$index])未设置($input[$index]);
}
此算法的内存需求为:


平均值(元素大小)*元素数量

您可以使用以下逻辑,该逻辑使用:

array\u map()
使用索引键值展平数组,并
serialize()
(stringify)最后一个数组元素,以便使用
array\u unique()
显示结果

然后,为了恢复字符串化的数组,即将其转换回数组,我们使用
unserialize()


工作

您可以使用以下逻辑,它使用:

array\u map()
使用索引键值展平数组,并
serialize()
(stringify)最后一个数组元素,以便使用
array\u unique()
显示结果

然后,为了恢复字符串化的数组,即将其转换回数组,我们使用
unserialize()


正在工作

要显示的任何代码。。。。任何使用逻辑的代码尝试?您的前后数据结构很有洞察力,但不是代码尝试。您需要进行循环并检查。没有内置的函数来做这件事。@GetSet我尝试了其他人的多种想法,比如array_unique(array_column($array,'Template');或者$temp=array_unique(array_列($array,'Template')$唯一\u arr=数组\u相交\u键($array,$temp);我将发布一个简单的方法为您做这件事很快与评论,让你跟随发生了什么。在我这样做之前,我需要您以代码形式发布输入数据结构,而不是
print\u r
var\u dump
。这是在您的问题中添加一些明确定义输入数组的代码。请澄清保留
[6]
键是否至关重要。我的意思是,你没有从中得到任何有用的东西,对吗?所以你对
[0]、[1]、[2]
也同样满意@要显示的该死的代码。。。。任何使用逻辑的代码尝试?您的前后数据结构很有洞察力,但不是代码尝试。您需要进行循环并检查。没有内置的函数来做这件事。@GetSet我尝试了其他人的多种想法,比如array_unique(array_column($array,'Template');或者$temp=array_unique(array_列($array,'Template')$唯一\u arr=数组\u相交\u键($array,$temp);我将发布一个简单的方法为您做这件事很快与评论,让你跟随发生了什么。在我这样做之前,我需要您以代码形式发布输入数据结构,而不是
print\u r
var\u dump
。这是在您的问题中添加一些明确定义输入数组的代码。请澄清保留
[6]
键是否至关重要。我的意思是,你没有从中得到任何有用的东西,对吗?所以你对
[0]、[1]、[2]
也同样满意@ShibeI我发现您最近的许多答案都是关于可以/应该作为重复答案关闭的问题。在发布答案之前,请检查是否存在重复内容,以便堆栈溢出志愿者策展人手动清除的冗余内容更少。请阅读……特别是:。我发现您最近的许多答案都是针对可以/应该作为重复答案关闭的问题。在发布答案之前,请检查是否存在重复内容,以便堆栈溢出志愿者策展人手动清除的冗余内容更少。请阅读…具体内容:。
Array
(
    [0] => page.html5
    [1] => page2.html5
    [6] => Array
        (
            [id] => 27
            [table] => tl_custom
            [type] => text
            [data] => Array
                (
                    [fragment] => example
                    [previewId] => 1
                    [isActive] => 1
                )

        )

)