How to transform this array?

Asked

Viewed 135 times

0

I have a form that in certain fields returns me several possibilities of data in a single index when I print the array. This situation can be seen in índices 3, 4, 5 e 7.

Array
(
    [0] => APARTAMENTO
    [1] => CASA
    [2] => CASA EM CONDOMINIO
    [3] => COBERTURA/COBERTURA HORIZONTAL
    [4] => CONJUNTO/SALA/LOJA
    [5] => FLAT/LOFT
    [6] => PREDIO COMERCIAL
    [7] => TERRENO/AREAS
)

I need to get to this result in PHP so that then I can start my queries in the database:

Array
(
    [0] => APARTAMENTO
    [1] => CASA
    [2] => CASA EM CONDOMINIO
    [3] => COBERTURA
    [4] => COBERTURA HORIZONTAL
    [5] => CONJUNTO
    [6] => SALA
    [7] => LOJA
    [8] => FLAT
    [9] => LOFT
    [10] => PREDIO COMERCIAL
    [11] => TERRENO
    [12] => AREAS
)

I arrived at this result with this form:

<input type="checkbox" name="tipo[]" value="APARTAMENTO" id="tp1">
<label for="tp1">Apartamento</label>

<input type="checkbox" name="tipo[]" value="CASA" id="tp2">
<label for="tp2">Casa</label>

<input type="checkbox" name="tipo[]" value="CASA EM CONDOMINIO" id="tp3">
<label for="tp3">Casa Condomínio</label>

<input type="checkbox" name="tipo[]" value="COBERTURA/COBERTURA HORIZONTAL" id="tp4">
<label for="tp4">Cobertura</label>

<input type="checkbox" name="tipo[]" value="CONJUNTO/SALA/LOJA" id="tp5">
<label for="tp5">Conjunto/Sala/Loja</label>

<input type="checkbox" name="tipo[]" value="FLAT/LOFT" id="tp6">
<label for="tp6">Flat/Loft</label>

<input type="checkbox" name="tipo[]" value="PREDIO COMERCIAL" id="tp7">
<label for="tp7">Prédio Comercial</label>

<input type="checkbox" name="tipo[]" value="TERRENO/AREAS" id="tp8">
<label for="tp8">Terreno/Área</label>

Passing only that as instruction:

$tipo = $_GET['tipo'];

For layout reasons, I cannot make these fields different, like um tipo por input. Any idea??

  • 1

    It depends on how you are grouping. Maybe a loop + explodes in / resolve, but I think you are grouping wrong.

  • 2

    You need to show the code/query that generates item1/item2, how is this form? How did you get to this array?. . . . If I may criticize a little, I notice that you usually lack this kind of information in your questions.

  • 1

    Since you’re gathering in the field value="OPÇÃO 1/OPÇÃO 2", a loop resolves. Another solution would be to mount via JS with hidden cmapos.

  • I don’t know how to treat arrays, can give me a way??

1 answer

4


A simple way would be using implode in the array using '/' and then turning into array with explode '/'. I just played in the ideone.

$array = array(
0 => 'APARTAMENTO',
1 => 'CASA',
2 => 'CASA EM CONDOMINIO',
3 => 'COBERTURA/COBERTURA HORIZONTAL',
4 => 'CONJUNTO/SALA/LOJA',
5 => 'FLAT/LOFT',
6 => 'PREDIO COMERCIAL',
7 => 'TERRENO/AREAS',
);

print_r( explode( '/' , implode( '/' , $array ) ) );
// output abaixo
Array
(
    [0] => APARTAMENTO
    [1] => CASA
    [2] => CASA EM CONDOMINIO
    [3] => COBERTURA
    [4] => COBERTURA HORIZONTAL
    [5] => CONJUNTO
    [6] => SALA
    [7] => LOJA
    [8] => FLAT
    [9] => LOFT
    [10] => PREDIO COMERCIAL
    [11] => TERRENO
    [12] => AREAS
)
  • Now that I have the array can I make an SQL query using it right?? Thanks for the help.

  • The powers of implode() sometimes frighten me. I was here thinking about something with array_walk_recursive() and such came this quick hack made ninja and... Vish...

  • First test was loop, then I saw that it was going to explode the /, then I decided to implode with her to solve both problems. And the sequence becomes small :)

Browser other questions tagged

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