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

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

藏色散人
Release: 2023-04-05 14:26:02
Original
2790 people have browsed it

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
)

)
Copy after login

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);
Copy after login

Output:

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

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

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

)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template