0

I have an object array of Categories and in each one i have a list of subcategories of the same type ie. item A -> itemA 1, itemA 2 etc. item B ... when i traverse through and match the sub cat, the data is not saved when changes are set. The code appears fine, any thoughts?

            foreach ($cats as $cat) {
            if($cat->getId() == $catChanged->getCategory_id()) {
                foreach ($cat->getSubCats() as $sc) {
                    if($sc->getId() == $id) {                           
                        var_dump($sc, $key);
                        $sc = $catChanged;                          
                        var_dump($sc, $key);    
                    }
                }               
            }   
        }   
        var_dump(serialize($cats));

As you can see var_dumps have been used showing the before and after however the $cats at the end is un-changed when it should be?

__________FIX_________

In the end i removed that element from the array and inserted the edited object in the same position. Works fine, thanks for your responses.

        foreach ($cats as $cat) {
            if($cat->getId() == $catChanged->getCategory_id()) {
                $i = -1;
                foreach ($cat->getSubCats() as $sc) {
                    $i++;
                    if($sc->getId() == $id) {                           
                        $tmp = AdminUtils::removeItem($cat->getSubCats(), $i);  //remove item
                        $cat->setSubCats(AdminUtils::insertArrayIndex($tmp, $catChanged, $i)); //re-insert at index
                        break(2); 
                    }
                }               
            }   
        }
2
  • $sc is just a local variable. Assigning to it does not change your original array. Commented Feb 4, 2012 at 20:40
  • 1
    It's an array so i think you should use for loop and change cat with pointing it by $cats[$i]=$catChanged Commented Feb 4, 2012 at 20:42

2 Answers 2

1

It's a known PHP gotcha. You can't change the value of an array object by changing the enumator. You'll need to change your code so that this line - $sc = $catChanged; - operates on the actual array object. It'll look something like $cats[$catnum]->getSubCats()[$subcatnum] = $catChanged;.

You can either do this by using integers to iterate through the array and subarrays, or you can stay with the existing approach but increment position integers as you traverse the arrays. The former is more elegant, but the latter also works.

1
  • Hi thanks for your help, gave me some ideas for the solution. Appears you can only go one level deep Commented Feb 4, 2012 at 21:48
0

Try this. You need to refer back to the $cat object.

foreach ($cat->getSubCats() as $sc) 
{
    if($sc->getId() == $id) 
    {                           
        var_dump($sc, $key);
         $cat->resetSubCat($id, $catChanged); 
    ]
}   

edit: i just reread your fix. You should put the array logic into the $cat object.

1
  • Keeping it separate allows for the logic to be used with any array, thanks anyway Commented Feb 8, 2012 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.