-1

A minimal sample array to represent the relevant portion of my query's result set:

[
    ['keyw' => 'sam , ram,shyam'],
    ['keyw' => 'sam,ram,shyam, mohan'],
    ['keyw' => 'sam, ram, shyam ,mohan,salman,babu , 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)); 
    }
}
?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
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

3 Answers3

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
  • 7,590
  • 9
  • 50
  • 58
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
0

You can split your column values on commas and consume the potential leading and trailing spaces around commas with preg_split(), then flip the values when pushing data into the result array to ensure uniqueness. Then when you want access the unique values, you can use array_keys() to convert the keys to values.

Code: (Demo)

$unique = [];
foreach ($db->getResult() as $row) {
    $unique += array_flip(preg_split(
        '/ *, */',
        $row['keyw'],
        0,
        PREG_SPLIT_NO_EMPTY
    ));
}
var_export(array_keys($unique));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136