【问题标题】:nested foreach loop in php causing duplicates in items appended to array during loopphp中的嵌套foreach循环导致循环期间附加到数组的项目重复
【发布时间】:2011-04-17 01:28:43
【问题描述】:

我在 php 特定论坛中多次问过这个问题,但没有回应。基本上,我使用的是 codeigniter 和一个名为 datamapper 的对象关系映射器。当您实例化一个对象时,它会将数据库表存储为对象,并将字段存储为属性。我正在尝试比较两个对象的属性以确定要删除哪些记录。基本上我有这样的东西:

http://blog.jeremymartin.name/2008/02/easy-multi-select-transfer-with-jquery.html#

我已经成功创建了我的添加函数。但现在我在编辑功能上苦苦挣扎。用户可能想要移除与某个类别相关联的父级或向该类别添加新的父级。所以我需要考虑这一点并相应地更新 categories_related_categories 连接表。

但是当我尝试在 foreach 循环中比较对象属性时,它会迭代内部循环两次并复制属性:

public function update(){
        $vanity_url = new VanityUrl();
        $vanity_url->where('user_id',$this->current_user()->id)->get();

        $zones = $this->input->post('zones');
        $zone = new Zone();
        $zone->where_in('name', $zones)->get();

        $subcategory = new Category();
        $subcategory->where('id',$this->uri->segment(4))->get();

        $old_parents = new Category();
        $old_parents = $subcategory->related_category->get();

        $unneeded_ids = array(); 

        if(!$this->input->post('catupload')){
            if($subcategory->save($zone->all)){
                redirect("blogs/$vanity_url->url/categories"); 
            }
            else {
                echo "Error occurred. Please try again.";
            }
        }
        else {
            $new_parents = new Category();
            $controller = $this->input->post('catupload');
            $new_parents->where_in('controller',$controller)->get();

            foreach($new_parents as $new){ 
                foreach($old_parents as $old){
                    if($new->id != $old->id){
                        array_push($unneeded_ids, $old->id);
                    }
                }
            }
            $subcategory->delete($subcategory->related_category->where_in('id',$unneeded_ids)->get());
if($subcategory->save(array($zone->all,$new_parents->all))){
                $this->session->set_flashdata('flash_message', 'The category has been successfully updated.');
                redirect("blogs/$vanity_url->url/categories");
            }

        }
    }

因此,我通过抓取与用户选择的内容不匹配的 id 并删除相关记录来删除用户不再需要的任何关系。

问题来了:

array(4) { [0]=> int(126) [1]=> int(127) [2]=> int(126) [3]=> int(127) } 

应该是这样的,因为 id 不可能重复:

 array(2) {[0]=> int(126) [1]=> int(127)}

感谢您的回复。

【问题讨论】:

    标签: php arrays codeigniter foreach object-comparison


    【解决方案1】:

    如果您想在数组中存储唯一元素,请将其用作关联映射:将元素用作键。

    也就是说,如果要将元素$id(整数)存储在数组$arr中,而不是

    array_push($arr, $id  );
    

    做:

    $arr[$id] = $id  
    

    $arr[$id] = 1; // or whatever
    

    之后,您可能需要array_keys

    【讨论】:

    • 直到你提到它,我才意识到函数 array_push 和关联数组形式之间存在差异。
    【解决方案2】:

    我没有测试过,但是你能用吗:

    foreach($new_parents as $new){ 
        foreach($old_parents as $old){
            if($new->id != $old->id && !in_array($old->id, $unneeded_ids){
                array_push($unneeded_ids, $old->id);
            }
        }
    }  
    

    ..检查 id 是否已在数组中列出?

    http://php.net/manual/en/function.in-array.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多