-1

This is my array:

Array ( [0] => sam [1] => ram [2] => shyam ) 
Array ( [0] => sam [1] => ram [2] => shyam [3] => mohan ) 
Array ( [0] => sam [1] => ram [2] => shyam [3] => mohan [4] => salman [5] => babu [6] => karan )

I want to remove duplicate names to get an output like sam,ram,shyam,mohan,salman,babu,karan. How can I achieve this?

<?php include 'config.php';
$db = new Database();
$db->select('post','*',null,null,'id',20); $result = $db->getResult(); if(count($result) > 0){ foreach($result as $row){ 
   $string = $row['keyw'];
   $string = $string;
    $array = explode(',', $string);
print_r(array_merge($array)); }} ?>
sam
  • 1
  • 1
  • There are a couple of different things you can do. What have you tried and where are you stuck? – scrappedcola Jul 22 '21 at 20:58
  • i want to remove duplicate name... bro i am new so i don't know...plz help – sam Jul 22 '21 at 21:04
  • i want output like sam,ram,shyam,mohan,salman,babu,karan – sam Jul 22 '21 at 21:05
  • If you search for your exact title you pretty much get handed the answer: https://stackoverflow.com/questions/307650/how-to-remove-duplicate-values-from-an-array-in-php. If you have already tried this solution then you should update you question with what you have tried. – scrappedcola Jul 22 '21 at 21:09
  • @scrappedcola Seems like these are separate arrays that need to be merged first. So is a bit different. – Gerard de Visser Jul 22 '21 at 21:17

2 Answers2

0

You can use array_unique function for removing duplicate array values. Like $arrayWithUniqueValues = array_unique($OriginalArray);

Because these seem like separate arrays, you can merge these first with array_merge, like: array_merge($array1, $array2).

The new array should only contain unique values then.

Gerard de Visser
  • 6,863
  • 9
  • 46
  • 56
0
<?php
include 'config.php';

$db = new Database();
$db->select('post','*',null,null,'id',20); $result = $db->getResult(); if(count($result) > 0){ 

$newArray  = array();
foreach ($result as $value) {
  $sh_genres = $value['keyw'];
  $sh_genres_array = array_map('trim', explode(',',$sh_genres));
  $newArray = array_merge($newArray , $sh_genres_array );    
}
$newUniqueArray = array_unique($newArray);

foreach($newUniqueArray as $links)  {
echo '<p>'.$links.'</p>';
 }}?>
sam
  • 1
  • 1