duanpang5583 2016-08-08 10:12
浏览 64

在对象数组中取消设置对象

I have a problem with remove items from an array with object, the same code run in other app. After looping the array $categories should be empty. The code bellow removes all children categories then removes the parent category if the user pass the second parameter as TRUE, if the parent doesn't have a child then remove it only.

//the $id of the category to be removed 
// $remove_children is state if you accept removing children categories  
function remove_category($id = null, $remove_children = false) {
    if ($this->MD->is_category($id) && is_bool($remove_children)) {
        //get all children category
        $children = $this->get_children_categories($id);
        if (!$children) {
            return $this->MD->remove($id, $this->categories_table);
        } else {
            if ($remove_children && is_array($children)) {
                unset($children['parent_category']);
                foreach ($children as $child) {
                    $removed = $this->MD->remove($child->id, $this->categories_table);
                    if ($removed) {
                        //problem here
                        unset($child);
                    }
                }
                //the $children is not empty after remove all the items
                if (empty($children)) {
                    return $this->MD->remove($id, $this->categories_table);
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    } else {
        return false;
    }
}
  • 写回答

1条回答 默认 最新

  • douxingti9307 2016-08-08 10:35
    关注

    For removing/setting null an array element we need to pass that particular index of array unset($children[$key]);-

    foreach ($children as $key=>$child) {
                        $removed = $this->MD->remove($child->id, $this->categories_table);
                        if ($removed) {
                            //problem here
                            unset($children[$key]);
                        }
                    }
    

    Hope this will help you.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度