开发者

nested foreach loop in php causing duplicates in items appended to array during loop

开发者 https://www.devze.com 2023-02-26 08:17 出处:网络
I asked this question several times in php specific forums but no response. Basically I am using codeigniter and an object relational mapper called datamapper. When you instantiate an object, it store

I asked this question several times in php specific forums but no response. Basically I am using codeigniter and an object relational mapper called datamapper. When you instantiate an object, it stores the database table as an object and the fields as properties. I am trying to compare the properties of two objects to determine which records to get rid of. Basically I have something like this:

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

I already created my add function successfully. But now I struggle with the edit function. A user may want to remove parents associated with a category or add new parents to the category. So I need to account for this and update the categories_related_categories join table accordingly.

But when I try to compare the object properties in a foreach loop, it iterates the inner loop twice and duplicates the properties:

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开发者_如何学Python_message', 'The category has been successfully updated.');
                redirect("blogs/$vanity_url->url/categories");
            }

        }
    }

So I delete any relationships that the user no longer wants by grabbing the ids that dont match what the user selected and removing associated records.

Here's the problem:

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

It should be like this instead, since there's no way that ids can be duplicated:

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

Thanks for response.


If you want to store unique elements inside an array, use it as an associative map: use the elements as keys.

That is, if you want to store the element $id (integer) in array $arr, instead of

array_push($arr, $id  );

do:

$arr[$id] = $id  

or

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

Afterwards, you'll probably need array_keys


I haven't tested it, but could you use:

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);
        }
    }
}  

..to check if the id is already listed in the array?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号