doushi9780 2016-02-23 10:54
浏览 8
已采纳

Php如果条件不适用于现有阵列

This sounds so simple but for some reason its not working on my end. I have an array which will check if values exist then will act accordingly

My array contains values and my condition is meant to check 2 conditions if value "a" is contained in the array at array index 0 and "b" doesnt exist in array then do this. if value "a" at index 0 and "b" does exist then run another block of code.

//THIS CONTAINS ALL THE FIELDS SELECTED
$report_cols = $_POST['report_cols'];
$percent_amt= explode(",",$report_cols);
$n_fields_arr = array();
$b=0;
//This checks if b exists in array
if (array_key_exists('`b`', $percent_amt)) 
{
  $b = 1;
}

if($percent_amt[0] == '`a`' && $b == 0)
{
$report_cols = str_replace("`a`,","",$report_cols);
$report_cols = str_replace(",`a`","",$report_cols);
$report_cols = str_replace(",,",",",$report_cols);
array_push($n_fields_arr,"a");
echo "done";    
}
if($percent_amt[0] == '`a`' && $b == 1)
{
$report_cols = str_replace("`a`,","",$report_cols);
$report_cols = str_replace(",`a`","",$report_cols);
$report_cols = str_replace(",,",",",$report_cols);
$report_cols = str_replace("`b`,","",$report_cols);
$report_cols = str_replace(",`b`","",$report_cols);
$report_cols = str_replace(",,",",",$report_cols);
array_push($n_fields_arr,"a","b");
echo "Done AB"; 
}

Error I am having is that its not recongizing the two && section and keeps running the first if statement. If there is a something am missing or a better method your help will be grately appreciated

  • 写回答

2条回答 默认 最新

  • dongwujie7477 2016-02-23 11:16
    关注

    explode() function explodes array with numeric keys.

    use in_array() to check b.

    if (in_array('b', $percent_amt)) 
    {
      $b = 1;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计