Notice : Array to String Conversion in

Asked

Viewed 3,702 times

4

I have a problem with the following code :

<select name="tipoPessoa" class="form-control">
    <?php 
        $tipos = array("Pessoa Fisica","Pessoa Juridica");
        $tipoSemEspaco = str_replace(" ","",$tipos);

        foreach ($tipos as $tipo):
        $esseEhOTipo = get_class($pessoa) == $tipoSemEspaco;
        $selecaoTipo = $esseEhOTipo ? "selected='selected'" : "";
    ?>
  // Linha 22 <option value="<?=$tipoSemEspaco?>" <?=$selecaoTipo?>>
                <?=$tipo?>
            </option>       

    <?php  endforeach ?>

</select>

When I open the browser console, it informs me a Notice:

Array To String Conversion in line 22

I am trying to assign the value of the field without the spaces and this giving this error.

3 answers

1

The reason for the mistake is because $tipoSemEspaco is a new array, which is independent of the $tipos.

An easy fix:

Add the index in the foreach:

foreach($tipos as $index => $tipo){ 
    /...
}

Then use it to get the respective $tipoSemEspaco:

<?=$tipoSemEspaco[$index]?>

In the end:

<select name="tipoPessoa" class="form-control">
    <?php 
        $tipos = array("Pessoa Fisica","Pessoa Juridica");
        $tipoSemEspaco = str_replace(" ","",$tipos);

        foreach ($tipos as $index => $tipo):
        $esseEhOTipo = get_class($pessoa) == $tipoSemEspaco;
        $selecaoTipo = $esseEhOTipo ? "selected='selected'" : "";
    ?>
  // Linha 22 <option value="<?=$tipoSemEspaco[$index]?>" <?=$selecaoTipo?>>
                <?=$tipo?>
            </option>       

    <?php  endforeach ?>

</select>

Another option would be to add something like:

$tiposPessoasDisponiveis = [
   0 => ['TextoHumano' => 'Pessoa Fisica', 'ValorMaquina' => 'PessoaFisica'],
   1 => ['TextoHumano' => 'Pessoa Juridica', 'ValorMaquina' => 'PessoaJuridica'],
];

Since there are few options and it does not tend to change this often, IN THIS CASE, this could be feasible, then:

<select name="tipoPessoa" class="form-control">
        <?php 

            $tipos = [
                0 => ['TextoHumano' => 'Pessoa Fisica', 'ValorMaquina' => 'PessoaFisica'],
                1 => ['TextoHumano' => 'Pessoa Juridica', 'ValorMaquina' => 'PessoaJuridica'],
            ];

            foreach ($tipos as $tipo):
                $esseEhOTipo = get_class($pessoa) == $tipo['ValorMaquina'];
                $selecaoTipo = $esseEhOTipo ? "selected='selected'" : "";
                ?>
                <option value="<?=$tipo['ValorMaquina']?>" <?=$selecaoTipo?>>
                    <?=$tipo['TextoHumano']?>
                </option>

            <?php  endforeach ?>

</select>

Personally I don’t think it’s very good, because it simply makes it confusing to understand what each thing is, are two different arrays and that has a direct relationship, that is my opinion, precisely for this I would prefer to do:

<select name="tipoPessoa" class="form-control">
        <?php 

            $tiposPessoasDisponiveis = ['Pessoa Fisica', 'Pessoa Juridica'];

            foreach ($tiposPessoasDisponiveis as $tipoPessoa){

                $tipoSemEspacamento = str_replace(' ', '', $tipoPessoa);

                $estaSelecionado = get_class($pessoa) == $tipoSemEspacamento;

                $atributoOptionHTML = '';
                $atributoOptionHTML .= 'value = "'.$tipoSemEspacamento.'"';
                $atributoOptionHTML .= $estaSelecionado ? 'selected' : '';

                ?>
                <option <?= $atributoOptionHTML ?>>
                    <?= $tipoPessoa ?>
                </option>

            <?php  } ?>

</select>

Finally, add the str_replace within the foreach, in this way it becomes clear what is the purpose of it, I renamed the functions so that try to present better their "functions", in my view. But logically this is totally optional.

0

Yes, in fact you are trying to assign an array as value, where it should be string. This is because in this expression:

$tipos = array("Pessoa Fisica","Pessoa Juridica");
$tipoSemEspaco = str_replace(" ","",$tipos);

You are trying to replace part of the array $tipos. The function that I thought most appropriate for your operation is the implode. It would be something like:

$tipos = array("Pessoa Fisica","Pessoa Juridica");
$tipoSemEspaco = implode(" ",$tipos);

And so you would actually have a string in the variable $tipoSemEspaco

  • Implode has a totally different idea than str_replace in this case, maybe the OP really wants this, but I don’t think so. The str_replace apparently made to remove spaces, ie the Pessoa Fisica go to PessoaFisica. Use the implode will stay Pessoa Fisica Pessoa Juridica, in the same string, it does not even live up to the name "semEspaco". This does not seem to be the case at all, but it may be that this is what the PB wants anyway.

0

You are assigning an array type parameter to a function that should receive the string type.

A suggestion for correction:

<select name="tipoPessoa" class="form-control">
    <?php 
        $tipos = array("Pessoa Fisica","Pessoa Juridica");

        foreach ($tipos as $tipo):
        $tipoSemEspaco = str_replace(' ', '', $tipo);
        $esseEhOTipo = get_class($pessoa) == $tipoSemEspaco;
        $selecaoTipo = $esseEhOTipo ? "selected='selected'" : "";
    ?>
    <option value="<?=$tipoSemEspaco?>" <?=$selecaoTipo?>>
                <?=$tipo?>
            </option>       

    <?php  endforeach ?>

</select>

In this correction I chose to be less invasive as possible, keeping its original code.

*I abstain from commenting on other issues.
The above code should solve the main problem.

Browser other questions tagged

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