Complex indexing for missing covariates

I think this is a common enough question, but I am still having issues with it.

I have a matrix of covariates \mathbf{X} (no intercept) where some some observations i=1,2,\ldots,N (rows) are missing for some covariates j=1,2,\ldots,J (columns). More generally, I have an array of size d = 1,2,\ldots,D of such matrices \mathbf{X}. I can define a matrix \mathbf{B} with binary elements indicating when the corresponding value of \mathbf{X} is missing. I also have n_j the number of missing observations for covariate j. So my data looks like

data {
  array[D] matrix[N,J] X ;
  array[D] matrix[N,J] B ;
  array[D,J] int n ;
}

I want to place prior a prior on x_{ij} when b_{ij}=1.

My initial thought based on what I’ve read in other posts is to do something like this

parameters {
  vector[D] beta0 ; // intercepts
  matrix[D,J] beta ; // covariate effects
  array[D,J] vector[n] x_mis // missing values
}
transformed parameters{
  array[D] matrix[N,J] X_complete ; // filled in covariate matrix
  {
    for(d in 1:D){
      for(j in 1:J){
        int k = 0 ;
        for(i in 1:N){
          if(B[d,i,j]==1){
            k += 1 ;
            X_complete[d,i,j] = x_mis[d,j,k] ;
          }else{
            X_complete[d,i,j] = X[d,i,j] ;
          }
        }
      }
    }
  }
}

Any help would be greatly appreciated.

Edit: fixed typos a few times.

What is your actual question?