Concatenate string in the middle of another using array loop

Asked

Viewed 68 times

1

My idea is to create a function where 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 am currently creating at hand, I want to do dynamically with the array. I hope you can explain, any doubt 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);

Intended 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


  }
  • compound arrays?

  • Yes, it was not very clear no. Why not give an input example that the function would receive and what output it should generate? Maybe it’ll make it easier to understand.

  • @Andersoncarloswoss added

  • Dude, I don’t get it yet, you want to create this array inside the loop?

  • I want the array to become this string, if the array has 3 elements I will have 3 fields_attributes and so on

1 answer

1


I believe that’s 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);
  • 1

    That’s right, thank you

  • @Igoroliveira was worth!

  • could you explain to me what the substar does?

  • @Igoroliveira he removes the last comma that is created in the loop.

  • That line $string .= "field_value: '".$dados."'},"; creates a comma at the end. With the substr I remove the last created comma.

  • @You want me to make a change?

  • 1

    Oh yes, I get it, no need to change, I’m already using

  • @Igoroliveira tranquilo! =)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.