从每列具有多个值的列返回唯一值

时间:2022-03-04 10:08:42

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

我有一个数据库表,其中一列有多个字符串存储在每个字段中,由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?

除了array_unique之外,一切似乎都在工作。我仍然在$ filter_final中获得重复的字符串。我究竟做错了什么?

1 个解决方案

#1


0  

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

在爆炸字符串之后,使用array_unique()来对数组中的唯一值进行求值。

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
)

#1


0  

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

在爆炸字符串之后,使用array_unique()来对数组中的唯一值进行求值。

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
)