0

I have a database table with a column where multiple strings are stored in each field, separated by a ;

I need to get a list of all unique values in that column. I know there has to be an easier way to do this, but I can't find my answer anywhere. Here is what I'm doing so far:

$result = mysql_query("SELECT DISTINCT column FROM modx_scripture_table WHERE column!=''", $con);

$filter_all = array();

while($row = mysql_fetch_array($result))
{
    $filter_all[] = $row;
}

 function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
} 
$filter_flat = array_flatten($filter_all);


function array_explode($array) {
  $result = array();
  foreach ($array  as  $key => $value) {
  $result[$key] = explode(';',$value); 
  }
  return $result;
}


$filter_sploded = array_explode($filter_flat);

$filter_full = array_flatten($filter_sploded);

$filter_final = array_unique($filter_full);

print_r($filter_final);

Everything seems to be working except the array_unique. I'm still getting duplicate strings in $filter_final. What am I doing wrong?

1 Answers1

0

After exploding the string use array_unique() for fnding unique values in an array.

Example:

$arr=array('1','2','3','4','5','3','1');
print_r(array_unique($arr));

Output:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
  • Thank you for your reply, but please read my original post more closely. I am already using array_unique. My problem is that it is not working. – user2020606 Jan 29 '13 at 13:38