How to remove duplicate values from an array in PHP
Use array_unique().
Example:
$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)
How to remove duplicate values from a multi-dimensional array in PHP
Here is another way. No intermediate variables are saved.
We used this to de-duplicate results from a variety of overlapping queries.
$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
how to remove duplicate elements from an array of arrays in PHP
Just use array_map and array_unique together.
<?php
$a = json_decode('{"2017-08-31":["5948a0dd21146a43fdcfef5a","5948a0dd21146a43fdcfef5a"],
"2017-08-22":["5948a0dd21146a43fdcfef5a"],
"2017-08-09":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"],
"2017-08-08":["59461ceae6179b19403c6a19","59461ceae6179b19403c6a19"]}', true);
echo json_encode(array_map("array_unique", $a));
Remove duplicate items from an array
The array_unique
function will do this for you. You just needed to add the SORT_REGULAR
flag:
$items_thread = array_unique($items_thread, SORT_REGULAR);
However, as bren suggests, you should do this in SQL if possible.
Related Topics
How to Get a File'S Extension in PHP
Apache Shows PHP Code Instead of Executing It
How to Prevent Duplicate Usernames When People Register
Only Variables Should Be Passed by Reference
Do Htmlspecialchars and MySQL_Real_Escape_String Keep My PHP Code Safe from Injection
Strtotime() Doesn't Work With Dd/Mm/Yyyy Format
Mvc For Advanced PHP Developers
Doctrine2: Best Way to Handle Many-to-many With Extra Columns in Reference Table
"Date(): It Is Not Safe to Rely on the System'S Timezone Settings..."
How to Repair a Serialized String Which Has Been Corrupted by an Incorrect Byte Count Length
Unicode Character in PHP String
How to Get the Cookies from a PHP Curl into a Variable
Return Single Column from a Multi-Dimensional Array
Automatically Deleting Related Rows in Laravel (Eloquent Orm)