如何在foreach控制器codeigniter之外获得价值

时间:2018-01-18 08:41:56

标签: php codeigniter foreach

我想从$data2['menu_down']获取价值以发送给我的观点,但我总是得到最后一个值$data2

public function index(){
    $data['menu'] = $this->M_home->tampil_menu_utama();
    foreach ($data['menu'] as $menus) {
        $idmenu = $menus->menuid;

        $data2['menu_down'] = $this->M_home->tampil_menu_down($idmenu);

    }
    $this->load->view('head',array_merge($data,$data2));
    $this->load->view('home');
    $this->load->view('foot');
}

1 个答案:

答案 0 :(得分:-1)

您需要将$data2作为数组;

public function index(){
    $data['menu'] = $this->M_home->tampil_menu_utama();
    $data2 = array(); //initialize your variable with array; 
    foreach ($data['menu'] as $menus) {
        $idmenu = $menus->menuid;

        $data2['menu_down'][] = $this->M_home->tampil_menu_down($idmenu);

    }
    $result['menu_down']=array_merge($data,$data2);
    $this->load->view('head',$result);
    $this->load->view('home');
    $this->load->view('foot');
}