【问题标题】:getting duplicate values under nested loop in php在php中的嵌套循环下获取重复值
【发布时间】:2018-03-20 17:32:10
【问题描述】:

我想在 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] => 5 [1] => 9 [2] => 10 )
数组 ( [0] => 5 [1] => 9 [2] => 10 )
数组 ( [0] => 5 [1] => 9 [2] => 10 )
数组 ( [0] => 5 [1] => 9 [2] => 10 )
数组 ( [0] => 5 [1] => 9 [2] => 10 )
数组 ( [0] => 5 [1] => 9 [2] => 10 )

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

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

【问题讨论】:

  • 那么看起来你不需要外循环,不是吗?
  • 需要保留外循环,因为外循环在嵌套循环之后有自己的位置,不共享循环代码,因为它太长了,虽然可能会填满整个问题页面
  • 我不知道该告诉你什么。如果您不希望 $categories 中的每个项目的页脚类别列表重复,则不应将其放入该循环中。
  • 发布您的 JSON,以便我们可以复制它...
  • @Edward 数组 ([0] => 5 [1] => 9 [2] => 10)

标签: php foreach nested-loops


【解决方案1】:

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) 移出,您会收到预期的结果。

【讨论】:

  • 请给我发送代码,我没有收到此更改
  • 需要保持相同的位置,因为它是一个动态菜单?
  • @JamezRoz 我不确定你想要实现什么。如果您在 $categories 上进行迭代并尝试从 $footer 类别创建链接,那么 $footer_category 将执行多次 $categories 有多少行。
  • 我认为我们需要做一些棘手的事情,但我对这方面的经验并不多,你能帮我一下吗-> stackoverflow.com/questions/35290643/…
猜你喜欢
  • 1970-01-01
  • 2014-10-22
  • 2013-12-27
  • 2017-07-24
  • 1970-01-01
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多