-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.

2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.