如何在多维数组中删除包含一个或多个重复值的数组条目?例如,如果我有:
array (size=4)
0 =>
array (size=3)
'food' => string 'bread' (length=5)
'color' => string 'white' (length=5)
'language' => string 'php' (length=3)
1 =>
array (size=3)
'food' => string 'rice' (length=4)
'color' => string 'purple' (length=6)
'language' => string 'c#' (length=2)
2 =>
array (size=3)
'food' => string 'pasta' (length=5)
'color' => string 'red' (length=3)
'language' => string 'php' (length=3)
3 =>
array (size=3)
'food' => string 'steak' (length=5)
'color' => string 'yellow' (length=6)
'language' => string 'ruby' (length=4)
由于数组[2]中有一个php条目,我想删除整个记录,以便我可以拥有
array (size=4)
0 =>
array (size=3)
'food' => string 'bread' (length=5)
'color' => string 'white' (length=5)
'language' => string 'php' (length=3)
1 =>
array (size=3)
'food' => string 'rice' (length=4)
'color' => string 'purple' (length=6)
'language' => string 'c#' (length=2)
2 =>
array (size=3)
'food' => string 'steak' (length=5)
'color' => string 'yellow' (length=6)
'language' => string 'ruby' (length=4)
我试过这段代码:
array_map("unserialize", array_unique(array_map("serialize", $array)));
并且它不起作用。我做错了什么?
答案 0 :(得分:0)
由于您的对象都不同(例如,它们都有不同的颜色),array_unique
不会减少元素的数量。
相反,按照language
属性键入数组元素:这将消除重复的php
条目,然后将该关联数组转换回索引数组:
foreach($array as $row) {
$result[$row['language']] = $row;
}
$result = array_values($result);
答案 1 :(得分:0)
使用<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.106</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
和array_intersect
函数的解决方案(将覆盖所有键的重复项):
array_merge
输出:
$arr = [
['food' => 'bread', 'color' => 'white', 'language' => 'php'],
['food' => 'rice', 'color' => 'purple', 'language' => 'c#'],
['food' => 'pasta', 'color' => 'red', 'language' => 'php'],
['food' => 'steak', 'color' => 'yellow', 'language' => 'ruby']
];
$values = []; // auxiliary array
foreach ($arr as $k => $a) {
if (array_intersect($values, $a)){
unset($arr[$k]); // removing the whole `entry` if it contains duplicates in any key
} else {
$values = array_merge($values, array_values($a));
}
}
print_r($arr);