Concatenate string in the middle of another using array loop

1

My idea is to create a function in which I will pass an array as a parameter with the format

array("c_digo"=>$cod,"nome"=>$nomecompleto)

And create a loop with this array so that it creates the same fields_attributes in this string below. I'm currently creating in the hand, I want to do dynamically with the array. I hope you can explain, any questions you may ask me

$queryObj = [
                'query' => 
                'mutation {
                  createCard(
                    input: {
                      pipe_id: '.$pipeid.'
                      fields_attributes: [{
                        field_id: "c_digo",
                        field_value: "'.$cod.'"
                      }{
                          field_id: "nome",
                          field_value: "'.$nomecompleto.'"
                      } {
                          field_id: "email",
                          field_value: "'.$email.'"
                      } {
                          field_id: "telefone",
                          field_value: "'.$telefone.'"
                      }]
                    }
                  ) {
                    card {
                      id
                    }
                  }
                }'
            ];
            $query = json_encode($queryObj);

Desired function

      public function createcard($array){
    // exemplo de array recebido: array("c_digo"=>$cod,"nome"=>$nomecompleto)
    // Create query object
//    deve criar esse queryobj já com os campos vindos do array ( atualmente ele ta preenchido manualmente)
    $queryObj = [
      'query' =>
        'mutation {
                  createCard(
                    input: {
                      pipe_id: '.$pipeid.'
                      fields_attributes: [{
                        field_id: "c_digo",
                        field_value: "'.$cod.'"
                      }{
                          field_id: "nome",
                          field_value: "'.$nomecompleto.'"
                      }]
                    }
                  ) {
                    card {
                      id
                    }
                  }
                }'
    ];

    $query = json_encode($queryObj);
//    deve retornar esta variavel $query


  }
    
asked by anonymous 04.05.2018 / 17:40

1 answer

1

I believe this is what you want:

function createcard($array){

    // coloquei esse valor pois ele não estava identificado no seu código
    $pipeid = 12334; // altere depois

    $queryObj = array();
    $string  = "mutation {createCard(input: { pipe_id: '$pipeid', fields_attributes: [";
    foreach($array as $key => $dados){
        $string .= "{field_id: '".$key."',";
        $string .= "field_value: '".$dados."'},";
    }
    $string = substr($string, 0, -1);
    $string .= "]}){card {id}}}";
    $queryObj['query'] = $string;
    return json_encode($queryObj);
}

$array = array("c_digo"=>"123","nome"=>"Igor Oliveira");

echo createcard($array);
    
04.05.2018 / 20:11