-1
array=[{"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

{"poi":"ytr","wert":"yui","iuyy":"yes"},

{"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

{"poi":"ytr","wert":"yui","iuyy":"yes"},

{"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

{"poi":"ytr","wert":"yui","iuyy":"yes"},

{"abc":"qwe","sdsd":"ewewe","fff":"gggg"}]

the output will be:

array=[{"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

{"poi":"ytr","wert":"yui","iuyy":"yes"}]

Please help me to solve the question If I am trying

this code

    <?php

    $array=[{"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

    {"poi":"ytr","wert":"yui","iuyy":"yes"},

    {"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

    {"poi":"ytr","wert":"yui","iuyy":"yes"},

    {"abc":"qwe","sdsd":"ewewe","fff":"gggg"},

    {"poi":"ytr","wert":"yui","iuyy":"yes"},

    {"abc":"qwe","sdsd":"ewewe","fff":"gggg"}];

    $array = array_unique($array);

    echo $array;

    ?>

then I am getting the run time error; while initializing please help I am getting this data from a very big existing function.

Fury
  • 4,643
  • 5
  • 50
  • 80
  • 1
    possible duplicate of [PHP: remove duplicate items in an array](http://stackoverflow.com/questions/5036403/php-remove-duplicate-items-in-an-array) – Bahman_Aries Jul 14 '15 at 09:27
  • 1
    possible duplicate of [How to remove duplicate values from an array in PHP](http://stackoverflow.com/questions/307650/how-to-remove-duplicate-values-from-an-array-in-php) – Fury Jul 14 '15 at 10:59

3 Answers3

0

Try this..

array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.

$getarray=json_decode(yourarray,true);

$array=array_unique($getarray);

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

Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
0

You could use the array_unique function, it takes in an array and returns another array without duplicates.

Here is an example :

<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
TehDude
  • 45
  • 7
0
Your array initialization is wrong, Try this :

    <?php

$array=[["abc"=>"qwe","sdsd"=>"ewewe","fff"=>"gggg"],

    ["poi"=>"ytr","wert"=>"yui","iuyy"=>"yes"],

    ["abc"=>"qwe","sdsd"=>"ewewe","fff"=>"gggg"],

    ["poi"=>"ytr","wert"=>"yui","iuyy"=>"yes"],

    ["abc"=>"qwe","sdsd"=>"ewewe","fff"=>"gggg"],

    ["poi"=>"ytr","wert"=>"yui","iuyy"=>"yes"],

    ["abc"=>"qwe","sdsd"=>"ewewe","fff"=>"gggg"]

];
$unique=array(); 
$sorted_unique=array(); 
    for($n=0;$n<count($array);$n++) 
    { 
        $unique[]=serialize($array[$n]); 
    } 
    $array1=array_unique($unique); 
    for($n=0;$n<count($array1);$n++) 
    { 

            $sorted_unique[]=unserialize($array1[$n]); 

    } 
var_dump($sorted_unique);

?>