in_array总是返回false?

时间:2017-01-31 14:03:15

标签: php jquery html

我想检查复选框一旦检查我是否正在将过滤器ID推入数组并且应该检查哪个过滤器ID为了这个我得到$ _GET ['filter']中的所有过滤器但是虽然我将这与$ filter数据进行比较意味着in_array总是返回false,请帮助我一个人

下面是HTML和PHP代码,

<?php echo $header; 
if(isset($_GET['filter']))
{
  $selected_filter[] = explode(",",$_GET['filter']);
  print_r($selected_filter);
  // $flag = 1;
} 
?>




  <div class="wrapper">
    <?php echo $column_left; ?>
    <div class="filterDiv">
      <h3>Filters by :</h3>
      <!-- <div class="flList">
        <p>price</p>
        <div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>         
      </div> -->
      <!-- <div class="list-group-item flList filter_options">
        <div class="flDrop price_Module"><div class="flDropDiv">price div</div></div>         
        <span id="amtmin"></span> - <span id="amtmax"></span>
        <input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
        <div id="slider-range"></div>
      </div> -->

      <div class="flList">
        <p>Categories</p>
        <div class="flDrop">
            <div class="flDropDiv category_fl">
              <?php foreach ($categories as $category) { 
                $category_name = explode("(",$category['name'])
              ?>
              <div class="flItems"><a href="<?php echo $category['href'] ?>"> <?php echo  $category_name[0]; ?> </a> </div>
              <?php }?> 
            </div>   
        </div>     
      </div>

             <?php //echo "<pre>"; print_r($filter_groups); die; ?>
      <?php foreach ($filter_groups as $filter_group) { ?> 
          <div class="flList">
            <p><?php echo $filter_group['name'];  ?></p>
              <div class="flDrop">
                <div class="flDropDiv">
                  <?php 
                    // $filter_string = "";
                    foreach ($filter_group['filter'] as $filter) {
                      // $filter_string = implode(',',$filter['']);
                      // print_r($filter_string);
                      if(isset($selected_filter) && in_array($filter['filter_id'],$selected_filter))
                      { ?>
                       <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>" checked> <?php echo $filter['name'] ?> 
                       <?php } else {?>
                          <input name="filter[]" type="checkbox" value="<?php echo $filter['filter_id']; ?>"> <?php echo $filter['name'] ?> 
                       <?php }?>
                  <?php }?> 
                </div>   
            </div>     
          </div>
      <?php }?>
      <div class="flList">
        <?php
        $amount_min = $min_product_price;$amount_max = $max_product_price;
        if(isset($_GET['amtmin']) && $_GET['amtmin']!=""){
          $amount_min = $_GET['amtmin'];
        }
        if(isset($_GET['amtmax']) && $_GET['amtmax']!=""){
          $amount_max = $_GET['amtmax'];
        }
        ?>
        <p>Price</p>
        <div class="flDrop price_Module">
            <div class="flDropDiv">
              <!-- <a class="list-group-item fltrHdng">Price</a> -->    
              <div class="price_slide">
                <!-- <label for="amount">Price range</label> -->                
                <input type="hidden" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
                <div id="slider-range"></div>
                <div class="cf mb10"></div>
                <span class="pull-left" id="amtmin"></span>  <span class="pull-right" id="amtmax"></span>                
              </div> 
            </div>   
        </div>
      </div>
      <div class="cf"></div>
        <?php
          if(isset($min_p) && ($max_p)){
          ?>
          <div class="filterSelectPrice filterSelect afr">
            <div id="fSprice" class="fSbtn">Rs.<?php echo $min_p; ?> - Rs.<?php echo $max_p; ?> <span class="clear fSp"></span></div>
          </div>
      <?php }   ?>
      <div class="filterSelect" id="auto_filter_values"></div>      
    </div>
  </div>

以下是我的jQuery代码:

<script type="text/javascript">

$('input[name^=\'filter\']').on('change', function() {
      filter = [];
      $('input[name^=\'filter\']:checked').each(function(element) {
        filter.push(this.value);
        $(filter).prop('checked',true);
      });
      // console.log(filter);return false;
      window.history.pushState("","",'<?php echo $action; ?>&filter=' + filter.join(','));
      //return false;
      $.ajax({
      url: '<?php echo $action; ?>&filter=' + filter,
      type: 'post',   
      cache: false,
      contentType: false,
      processData: false,   
      beforeSend: function() {
        $('#content').block({message:'<img src="<?php echo HTTP_SERVER; ?>image/ajax-loader.gif">'}); 
        //$("#content").fadeOut("slow");
      },
      complete: function() {     

        // $(this).remove();
        //$("#content").fadeIn("slow");
        $('#content').unblock();
      },    
      success: function(data) {
        $("body").empty().append(data);
      },      
      error: function(xhr, ajaxOptions, thrownError) {
        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
      }
    });
  });
</script>

1 个答案:

答案 0 :(得分:0)

在执行[]时,您正在定义多维数组:

$selected_filter[] = explode(",",$_GET['filter']);

in_array()正在寻找顶层,这是一个阵列,所以它不会起作用。只是做:

$selected_filter = explode(",",$_GET['filter']);