douqian3712
2015-02-03 21:50
浏览 276
已采纳

基于sql查询从数组中删除元素

$_SESSION['lines'] is populated from a textbox that a user pastes into. Before I do anything with the data, I want to run it through a filter to see if any of the values they've input are NOT in the database.

foreach ($_SESSION['lines'] as $q) {
$multiSupplierQuery      = "SELECT distinct supplier from allparts where quotePartNumber = '$q'";
$multiSupplierResult    = mysqli_query($con, $multiSupplierQuery);

while ($row = mysqli_fetch_array($multiSupplierResult)) {
  $multiSupplier = $row['supplier'];
}
 if (!multiSupplier) {
  unset($_SESSION['lines'[]);
 }
}

The crux of this revolves around this statement:

 if (!multiSupplier) {
  unset($_SESSION['lines'[]);
}

What I'm trying to say is: each time we cycle through this, if multiSupplier doesn't exist, remove this particular element from the array.

my unset syntax is wrong though... how do I make it right?

图片转代码服务由CSDN问答提供 功能建议

$ _ SESSION ['lines'] 从用户粘贴到的文本框中填充 。 在我对数据做任何事情之前,我想通过过滤器来运行它,看看它们输入的任何值是否都不在数据库中。

  foreach($  _SESSION ['lines'] as $ q){
 $ multiSupplierQuery =“select allparts from distinctPartNumber ='$ q'”; 
 $ multiSupplierResult = mysqli_query($ con,$ multiSupplierQuery); 
 
while(  $ row = mysqli_fetch_array($ multiSupplierResult)){
 $ multiSupplier = $ row ['supplier']; 
} 
 if(!multiSupplier){
 unset($ _ SESSION ['lines'[]); 
  } 
} 
   
 
 

这个问题的关键在于这句话:

  if(!multiSupplier){  
 unset($ _ SESSION ['lines'[]); 
} 
   
 
 

我想说的是:每次循环 通过这个,如果multiSupplier不存在,从数组中删除这个特定的元素。

我的 unset 语法错了虽然...如何 我做对了吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dragon19720808 2015-02-03 22:08
    已采纳

    You probably want to add a $ to your if statement. Change

    if (!multiSupplier) {
    

    to

    if (!$multiSupplier) {  
    

    Also, to unset from the $_SESSION you need a key. Try changing

    foreach ($_SESSION['lines'] as $q) {
    

    to

    foreach ($_SESSION['lines'] as $key=>$q) {
    

    and then

    unset($_SESSION['lines'][$key]);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douyongwan5946 2015-02-03 21:55

    It's not your unset usage, you just have an unnecessary opening square bracket.

    unset($_SESSION['lines'[]);
    

    should be

    unset($_SESSION['lines']);
    

    And you'll be good to go.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题