How to remove duplicate values from Array in PHP
How to remove duplicate values from Array in PHP
In this article, we will see how to solve "How to remove duplicate values from Array in PHP".
We can use the PHP array method to remove duplicate values from the array in PHP. array methods are default PHP methods that are used to manipulate arrays. To remove duplicate data from an array we can use array_unique to remove duplicate data from the indexed array and array_unique method with map method to remove from an associative array.-
Remove duplicate elements from indexed array in PHP
$myArray = [11,34,56,78,34,78]; $myArrayNew = array_unique($myArray); print_r($myArrayNew);
//Output
Array ( [0] => 11 [1] => 34 [2] => 56 [3] => 78 )We can use the array_unique method to remove duplicate records from the array. In our case, we have two duplicate records 34 and 78. After using array_unique() method we get array elements with 11, 34, 56, and 78 so we can see that this array method removes duplicate records from indexed arrays.
-
Remove duplicate elements of associative array in PHP
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'john'), 2=>array('a'=>1,'b'=>'john') ); $unique = array_map("unserialize", array_unique(array_map("serialize", $result))); print_r($unique);
//Output
Array ( [0] => Array ( [a] => 1 [b] => Hello ) [1] => Array ( [a] => 1 [b] => john ) )It removes array elements if both keys and values are the same in the associative array else it treats as a unique element if keys and values are different with compared to other elements
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Php
- Type hinting in PHP
- How to check If an element exists in Array in php
- Service container sample code in PHP mvc framework
- Convert an array to string in PHP
- You are trying to access an array as object
- How to displaying ckeditor save HTML data from SQL database
- How to add new item to array in PHP
- How to check file is an image in php
- Array to string conversion error in PHP
- Show errors in php
- How to set php executable path php.validate.executablePath in vscode
- How to get image tmp_name in PHP
- If statement with multiple conditions in php
- User-defined functions in php
- How to remove duplicate items from an array in PHP
- Convert string to sha512 in PHP
- Shorthand for isset() in php
- How to add php8.2 in ubuntu
- How to add elements to an empty array in PHP?
- Convert comma-separated string into array in PHP
- Page view counter in PHP
- How to increment variable value by 1 in php
- Delay code execution in PHP
- How to display data in ckeditor?
- How to get date with day, month, year, weekdays from string "2021/08/12" in php