douqian3712 2015-02-03 13: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 14: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 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?
  • ¥15 ctrl win alt 键一直触发
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部