Arie Sastra
Arie Sastra

Reputation: 183

Get Distinct data from array in php

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

Answers (4)

Chirag Patel
Chirag Patel

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

Poiz
Poiz

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

devpro
devpro

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));
?>

DEMO

Upvotes: 0

Mack4Hack
Mack4Hack

Reputation: 119

array_unique() — Removes duplicate values from an array.

Please refer : http://php.net/manual/en/function.array-unique.php

Upvotes: 2

Related Questions