从mysql表中存在的数组中删除元素

时间:2017-03-07 19:19:17

标签: php mysql arrays

如何从mysql表中存在的数组中删除元素

 $word = array('من','الى','على','عن','في','-','@','#','$','(',')','[',']','و');// create an array
 $explode = explode(" ",$title);
 $search = array_diff($explode,$word);//delete element if exist in the array word
 $implode = implode(",",$search);//implode the array search

 $query = mysqli_query($conn,"select db_key from tbl_keyword where db_key IN ('$implode')")
        or die(mysqli_error($conn));// select from the table where column in $implode
 $found = array();
 while($row = mysqli_fetch_array($query)){
     $found[] = $row['db_key'];
 }
 $not_found = array_diff($search, $found); print_r($not_found)."<br/>";
 $imp = implode(",",$not_found);
 $insert = mysqli_query($conn, "insert into tbl_keyword(db_key,db_tid) 
                                values ('$imp','$last_id')")
         or die(mysqli_error($conn));

我的问题出现在这部分代码中我要从表中存在的数组中删除元素我不想在表中有重复的单词

$found = array();
while($row = mysqli_fetch_array($query)){
    $found[] = $row['db_key'];
}
$not_found = array_diff($search, $found);print_r($not_found)."<br/>";
$imp = implode(",",$not_found);
$insert = mysqli_query($conn,"insert into tbl_keyword(db_key,db_tid) 
                              values ('$imp','$last_id')")
          or die(mysqli_error($conn));

当我打印$not_found时,我有一个空数组

我该如何解决这个问题 从mysql数据库中存在的数组中删除元素 enter image description here

1 个答案:

答案 0 :(得分:0)

这不是一个非常好的解决方案,但是你的非漂亮的非1NF表结构需要它。为IN交换FIND_IN_SET

SELECT `db_key` FROM `tbl_keyword` WHERE FIND_IN_SET(`db_key`,'$implode')