Notice: Array to String conversion in

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'm trying to assign the value in the field value without the spaces and giving this error.

    
asked by anonymous 13.03.2017 / 04:49

3 answers

1

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

An easy fix:

Add index to foreach :

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

Then use it to get its $tipoSemEspaco :

<?=$tipoSemEspaco[$index]?>

At 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 this:

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

Since there are few options and do 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 do not think this is very good , because it simply makes it confusing to understand what each thing is, it's two distinct arrays and it has a direct relationship, that's my opinion, :

<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, adding str_replace to foreach , this way it's clear what its purpose is, I've renamed the functions so that you can try to better present your "functions", in my view . But of course this is totally optional.

    
13.03.2017 / 05:51
0

Yes, in fact you are trying to assign an array 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 $tipos array. The function I thought best suited for your operation is implode . It would look something like:

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

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

    
13.03.2017 / 05:36
0

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

A suggested 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 fix I opted to be as non-invasive as possible while retaining your original code.

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

    
13.03.2017 / 08:44