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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)