Reputation: 183
I have duplicate date data in my array like this
$data {
2016-07-21,
2016-07-22,
2016-07-23,
2016-07-21,
2016-07-22,
2016-07-23
}
In MySql syntact I just put DISTINCT in query so that date will not duplicated, how can I "DISTINCT" data in PHP with this case ?
Upvotes: 1
Views: 2742
Reputation: 177
You can get DISTINCT data from array using "array_unique() Function" For Example,
<?php $a=array("a"=>"red","b"=>"green","c"=>"red"); print_r(array_unique($a));?>
Upvotes: 0
Reputation: 7617
Simply run your Array through
array_unique()
and You'd get Unique Values like so:
<?php
$data =[
'2016-07-21',
'2016-07-22',
'2016-07-23',
'2016-07-21',
'2016-07-22',
'2016-07-23',
];
$uniqueData = array_unique($data);
var_dump($uniqueData);
// YIELDS::
array (size=3)
0 => string '2016-07-21' (length=10)
1 => string '2016-07-22' (length=10)
2 => string '2016-07-23' (length=10)
Upvotes: 0
Reputation: 16117
In php, you can use array_unique()
method for removing duplication.
<?php
$data = [
'2016-07-21',
'2016-07-22',
'2016-07-23',
'2016-07-21',
'2016-07-22',
'2016-07-23'
];
print_r(array_unique($data));
?>
Upvotes: 0
Reputation: 119
array_unique()
— Removes duplicate values from an array.
Please refer : http://php.net/manual/en/function.array-unique.php
Upvotes: 2