Codeigniter 3.11 problems with foreach and inserts

im trying to learn codeigniter and im passing an old php project to codeigniter but im having problem with my routine in foreach, because if i run var_dump it is giving the value but when im sending to the insert it is giving a value of zero, my view and controller it is fine, but the foreach routine it is giving me a problem, any help would be appreciated:

Model example

public function addcustcfdi($data) {
                
                //Buscamos la calle si existe tomamos el ID si no, insertamos
                $this->db->select('id');
                $this->db->from('facturacion_calle');
                $this->db->where('calle',$data["calle"]);
                $query= $this->db->get();

                foreach ($query->result_array() as $row)
                {
                   $row['id'];
                }
                if ($row['id'] == 0)
                {
                    $queryinsert = $this->db->query("INSERT INTO facturacion_calle (calle) VALUES ('".$data["calle"]."'')");
                }
                $this->db->select('id');
                $this->db->from('facturacion_calle');
                $this->db->where('calle',$data["calle"]);
                $query= $this->db->get();
                foreach ($query->result_array() as $row)
                    {
                        $id_calle = $row['id'];
                    }

The weird issue it is in Core PHP it works fine with this syntax:

//Buscamos la calle si existe tomamos el ID si no, insertamos
$query = sprintf("SELECT id FROM $tbl_2 WHERE calle='%s'",$calle);
    $result = $db->query($query);
    if (!$result || ! ($row = mysqli_fetch_assoc($result))) {
        $query = sprintf ("INSERT INTO $tbl_2 (calle) VALUES ('%s')",$calle);
        $db->query($query);
    }
    $query = sprintf("SELECT id FROM $tbl_2 WHERE calle='%s'",$calle);
    $result = $db->query($query);
    while ($row = mysqli_fetch_assoc($result))
    {
        $id_calle = $row['id'];
    }   

Hope that someone can help me out, warm regards!