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);
}
}
}
}
$sc
is just a local variable. Assigning to it does not change your original array.