0

I need some help. I need to remove duplicate data as per key value using PHP. I am explaining my code below.

$orginalArr = array(
  array("id"=>1,"name"=>"Raj"),
  array("id"=>1,"name"=>"Raj"),
  array("id"=>2,"name"=>"Ram"),
  array("id"=>2,"name"=>"Ram"),
  array("id"=>3,"name"=>"Rahul")
);
echo json_encode($orginalArr);

Here I need to remove the data as per id, meaning if id value is same then one set of data will be removed. Please help me.

richhallstoke
  • 1,519
  • 2
  • 16
  • 29
  • 5
    Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – sidyll Dec 09 '16 at 12:13

1 Answers1

0

Just iterate through values and push it to the new array.
Example:

<?php
$orginalArr=array(array("id"=>1,"name"=>"Raj"),array("id"=>1,"name"=>"Raj"),array("id"=>2,"name"=>"Ram"),array("id"=>2,"name"=>"Ram"),array("id"=>3,"name"=>"Rahul"));

$newArray = array(); 

foreach ( $orginalArr as $arr )
    $newArray[$arr['id']] = array(
        'id' => $arr['id'], 
        'name' => $arr['name']); 



echo json_encode(array_values($newArray));
?>
malutki5200
  • 1,092
  • 7
  • 15