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

基于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?

  • 写回答

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]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004
  • ¥15 请问如何查看手机root记录?
  • ¥15 商城小程序订单号重复
  • ¥15 学校优化算法sbo和蚁群算法怎么结合