在php中嵌套循环下获取重复值

时间:2018-03-20 17:32:11

标签: php foreach nested-loops

我想在1循环下使用嵌套循环但是我得到重复的结果可以有人帮忙吗?

这是我的代码

 <ul>
                    <?php
                        $footer_category =  json_decode($this->db->get_where('general_settings',array('type' => 'footer_category'))->row()->value);
                        $categories = $this->db->get('category')->result_array();
                        foreach($categories as $row){
                        foreach($footer_category as $cat){
                    ?>
                    <li>                    
                    <a href="javascript:void(0);">
                    <?php
                    print_r($cat);
                    //      echo $this->crud_model->get_type_name_by_id('category',$cat,'category_name');
                    ?>
                    </a>
                        <div>
                        <div class="col-md-12">
                            <?php
                                $subs = $this->db->get_where('sub_category',array('category'=>$row['category_id']))->result_array();
                                foreach($subs as $row1){
                                    $this->db->limit(4);
                                    $this->db->order_by('product_id','desc');
                                    $products = $this->db->get_where('product',array('sub_category'=>$row1['sub_category_id'],'status' =>'ok'))->result_array();
                            ?>
                                <div class="col-md-12"><h3 class="text-center" style="background:#EAEAEA;"><?php echo $row1['sub_category_name']; ?></h3></div>
                                <?php
                                    foreach($products as $row2){
                                        if($this->crud_model->is_publishable($row2['product_id'])){
                                ?>
                                    <div class="col-md-3">
                                        <div class="menu_box">
                                            <div class="img_menu_box" style="background:url('<?php echo $this->crud_model->file_view('product',$row2['product_id'],'','','no','src','multi','one') ?>') no-repeat center center; background-size: 100% auto;">
                                            </div>

                                        <a href="<?php echo $this->crud_model->product_link($row2['product_id']); ?>">
                                            <?php echo $row2['title']; ?>
                                        </a>

                                        </div>
                                    </div>
                                <?php
                                        }
                                    }
                                ?>
                            <?php
                                }
                            ?>
                        </div>
                        </div>
                    </li>
                    <?php 
                    }
                     ?>

                    <?php
                        }
                    ?>


                    </ul>

这里是print_r($ cat)的重复结果;
5
9
10个
5
9
10个
5
9
10个
5
9
10个
5
9
10个
5
9
10个
这里是json print_r($ footer_category)中的重复结果;
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)
数组([0] =&gt; 5 [1] =&gt; 9 [2] =&gt; 10)

  

然而print_r的预期结果($ cat);是       5
      9
      10

我想保留两个循环for must,因为两个循环在代码中都有自己的位置,

1 个答案:

答案 0 :(得分:0)

print_r($cat);

foreach($footer_category as $cat){ 这打印出你期望的内容

  

5 9 10

但这是foreach($categories as $row)  并且这个循环重复foreach($footer_category as $cat) 6次,提供重复的结果。 如果您从foreach($footer_category as $cat){移动foreach($categories as $row),则会收到预期结果。