簡體   English   中英

in_array 總是返回 false?

[英]in_array always returns false?

我想在檢查后選中復選框,因為我將過濾器 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>

您在執行[]時定義了一個多維數組:

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

in_array()正在查看頂層,它是一個數組,因此它不起作用。 做就是了:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM