How to remove duplicate data of JSON object using PHP

Related searches

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.

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

How to remove duplicate values in a PHP array, This question already has an answer here Remove duplicates from an array of objects in JavaScript Since you're using Json.NET to work with JSON data. It will be better to have a class generated for the data, this will provide a basic template for your data to work on it. JsonConvert would convert the JSON notations into that class object, and will convert the class object into a JSON string notation. Your data can have an class object

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

How to remove duplicate data from json array using Javascript or , Since you're using Json.NET to work with JSON data. It will be better to have a class generated for the data, this will provide a basic template� The purpose of this article is to share with you the best ways to remove duplicate objects from JavaScript Array based on specific property/key. We will also analyze their performance in terms of execution time for different lengths of the array. If you want to learn how to remove duplicate PRIMITIVE values from JavaScript array check this article.

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

how to remove duplicate records in a json file., In this article, we will remove an object if the name property of an object JSON Remove Duplicate Objects (by name property) var data = [{. Anyone of the object properties can be used as a key. The key is extracted from the object and used as the index of the new temporary array. The object is then assigned to this index. This approach will remove the duplicate objects as only one of each object of the original array will get assigned to the same index.

JSON Remove Duplicate Objects, how to check if PHP variable contains non-numbers? how to check laravel version � how to check the size of mysql database in phpmyadmin � how to check � Big Data Cloud Database We can have duplicate keys in a JSON object, and it would still be valid. // Despite the firstName key being repeated, this is still valid JSON

php remove duplicates from array Code Example, The array_unique () function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Note: The returned array will keep the first array item's key type.

Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function. Loop Through the Result

Comments
  • 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" } }`
  • Make sure you don't forget the last line $result = json_encode( $array ); to transform your PHP array back to a JSON string!
  • Ok..i did as per you but the index of the json data is not coming serially.
  • 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.