5

I need one help.I have some JSON type data and i want to remove the duplicate set of data using PHP.I am explaining my code below.

data=[
   {'member_name':member1,'no_of_users':20},
    {'member_name':member1,'no_of_users':20},
    {'member_name':member1,'no_of_users':20},
    {'member_name':member2,'no_of_users':10},
   {'member_name':member2,'no_of_users':10},
   {'member_name':member3,'no_of_users':30},
]

my php side code is given below.

$res[]=array('member_name'=>$member,'no_of_members'=>$rowno['cnt']);
$result=var_dump( array_unique( $res, SORT_REGULAR ) );
//$result = json_decode($array, TRUE );
print json_encode($result);

Here we can see many duplicate data available.I need to remove only the duplicate data from this JSON object using PHP.Please help me.

3 Answers3

9

First json_decode the JSON string, so we can work with it in PHP. Then you should use array_unique with the flag SORT_REGULAR to remove all duplicates and lastly json_encodeit again to a JSON string. Here's a working example:

$data = '[
   {"member_name":"member1","no_of_users":20},
    {"member_name":"member1","no_of_users":20},
    {"member_name":"member1","no_of_users":20},
    {"member_name":"member2","no_of_users":10},
   {"member_name":"member2","no_of_users":10},
   {"member_name":"member3","no_of_users":30}
]';

// Make a PHP array from the JSON string.
$array = json_decode( $data, TRUE );

// Only keep unique values, by using array_unique with SORT_REGULAR as flag.
// We're using array_values here, to only retrieve the values and not the keys.
// This way json_encode will give us a nicely formatted JSON string later on.
$array = array_values( array_unique( $array, SORT_REGULAR ) );

// Make a JSON string from the array.
$result = json_encode( $array );

Edit: Based on your edit in your question: Don't assign $result to a var_dump. Replace $result=var_dump( array_unique( $res, SORT_REGULAR ) ); by $result=array_unique( $res, SORT_REGULAR );

Rein
  • 3,211
  • 3
  • 16
  • 21
  • Ok,Now how can i view these data in same format.when i printed the out put its coming like this` array(3) { [0]=> array(2) { ["member_name"]=> string(15) "Medilink 1" ["no_of_members"]=> string(3) "383" } [7]=> array(2) { ["member_name"]=> string(19) "Medilink2" ["no_of_members"]=> string(2) "24" } [11]=> array(2) { ["member_name"]=> string(16) "Medilink 3" ["no_of_members"]=> string(1) "8" } }` –  Jan 25 '16 at 07:34
  • Make sure you don't forget the last line `$result = json_encode( $array );` to transform your PHP array back to a JSON string! – Rein Jan 25 '16 at 07:38
  • Ok..i did as per you but the index of the json data is not coming serially. –  Jan 25 '16 at 08:44
  • That happens because PHP will keep the keys from the original array after using `array_unique`. This can be resolved by only getting the values from the returned array, by using `array_values`. I updated the code in my answer. This will give you the exact same format as your input. – Rein Jan 25 '16 at 09:24
2

I was faced with the same challenge, so I came up with this simple solution.

<?php
//UniqueValues.php
class UniqueValues{
    #The data Array
    private $dataArray;
    /*
        The index you want to get unique values.
        It can be the named index or the integer index.
        In our case it is "member_name"
    */
    private $indexToFilter;

    public function __construct($dataArray, $indexToFilter){
        $this->dataArray = $dataArray;
        $this->indexToFilter = $indexToFilter;
    }
    private function getUnique(){
        foreach($this->dataArray as $key =>$value){
            $id[$value[$this->indexToFilter]]=$key;
        }
        return array_keys(array_flip(array_unique($id,SORT_REGULAR)));
    }
    public function getFiltered(){
        $array = $this->getUnique();
        $i=0;
        foreach($array as $key =>$value){
            $newAr[$i]=$this->dataArray[$value];
            $i++;
        }
        return $newAr;
    }
}
?>

include the class in your invocation code and that's all

<?php
//index.php
include_once('UniqueValues.php');
#Your JSON data
$data = '[
   {"member_name":"member1","no_of_users":20},
    {"member_name":"member1","no_of_users":20},
    {"member_name":"member1","no_of_users":20},
    {"member_name":"member2","no_of_users":10},
   {"member_name":"member2","no_of_users":10},
   {"member_name":"member3","no_of_users":30}
]';
#Convert your JSON to Array
$array = json_decode( $data, TRUE );

/*
Create an object by passing the "Two Dimension Array" in this case "$array" and 
the "index" in this case "member_name" that you want to get the Unique Values
*/
$supper = new UniqueValues($array,"member_name");

/*
Get the unique valued array by calling the getFiltered() function
and encode it to JSON
*/
$result = json_encode( $supper->getFiltered() );

#Let the World See it :)
echo $result;
?>
JPA
  • 164
  • 3
  • 6
1

Here, how can I solve this. See with example. make json unique

code part:

<?php 
    $depositeArray = array( 'deposite'=>array(
            array('email'=>"sajib@gmail.com", 'deposite'=>0),
            array('email'=>"avi@gmail.com", 'deposite'=>0),
            array('email'=>"iqbal@gmail.com", 'deposite'=>0),
            array('email'=>"balla@gmail.com", 'deposite'=>0),
            array('email'=>"sajib@gmail.com", 'deposite'=>0),
            array('email'=>"razib@gmail.com", 'deposite'=>0)
            ),
            'total'=>0);
    $depositeArray = json_encode($depositeArray);
    $depositeArray = json_decode($depositeArray,true);
    $depositeArrayNew = Json_Super_Unique($depositeArray['deposite'],'email');
    $depositeArray['deposite'] = $depositeArrayNew ;
    echo json_encode($depositeArray);
    function Json_Super_Unique($array,$key){
       $temp_array = array();
       foreach ($array as &$v) {
           if (!isset($temp_array[$v[$key]]))
           $temp_array[$v[$key]] =& $v;
       }
       $array = array_values($temp_array);
       return $array;
    }
?>
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
Jakir Hossen
  • 451
  • 4
  • 13