Home  >  Article  >  Backend Development  >  How to remove duplicate values ​​from multidimensional array in PHP?

How to remove duplicate values ​​from multidimensional array in PHP?

藏色散人
藏色散人Original
2019-03-07 14:25:052706browse

During the development of PHP projects, sometimes we need to remove duplicate values ​​from multi-dimensional arrays in PHP frameworks, such as laravel, codeigniter and zend, etc. Below, we will introduce to you how to delete duplicate values ​​from PHP multi-dimensional arrays with specific examples.

How to remove duplicate values ​​from multidimensional array in PHP?

In the example below, there is a simple multi-dimensional array with repeated values, then we can pass PHP array_map() and The array_unique() function is used to obtain unique values ​​from multi-dimensional arrays, that is, to remove duplicate values ​​from multi-dimensional arrays.

PHP Multidimensional Array:

Array
(
[0] => Array
(
[0] => php
[1] => sql
)

[1] => Array
(
[0] => javascript
[1] => c
)

[2] => Array
(
[0] => php
[1] => sql
)

[3] => Array
(
[0] => c++
[1] => java
)

)

Remove duplicate values:

$myArray = Array(
          Array('php','sql'),
          Array('javascript','c'),
          Array('php','sql'),
          Array('c++','java')
        );

$myArray = array_map("unserialize", array_unique(array_map("serialize", $myArray)));

print_r($myArray);

Output:

Array
(
[0] => Array
(
[0] => php
[1] => sql
)

[1] => Array
(
[0] => javascript
[1] => c
)

[3] => Array
(
[0] => c++
[1] => java
)

)

Related recommendations: "PHP Tutorial"

This article is about the method of deleting duplicate values ​​from PHP multi-dimensional arrays. It is simple and easy to understand. I hope Help those in need!

The above is the detailed content of How to remove duplicate values ​​from multidimensional array in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn