How to send Blank Array when no data found? How to send Blank Array when no data found? codeigniter codeigniter

How to send Blank Array when no data found?


You should initialize the $sArr with cities key as empty array. If the cities are then found of the state, the cities key will be overwritten in the loop else you will get empty values.

                            $sArr = array();                            $sArr = array(                                'mf_state_id' => $s['mf_state_id'],                                'name' => $s['name'],                                'status' => $s['status'],                                'cities' => array(),    //Initialize with empty array here.                            );


Try this, If state have no cities or empty then other code does not execute.

if(count($city)>0){    foreach ($city as $c) {        $cArr = array(            'mf_city_id' => $c['mf_city_id'],            'mf_state_id' => $c['mf_state_id'],            'name' => $c['name'],            'status' => $c['status'],            'active' => $c['active']        );    }}else{    $cArr = array();}