Concatenate variable with another PHP variable

Asked

Viewed 123 times

1

Mine inputs are being generated from a repetition of Javascript naming them as follows: input.setAttribute("name",`tubo_${i}`) and input.setAttribute("name",`largura_${i}`)

My problem is when I need to handle and separate these values in PHP, I get the items via $_POST.

print_r($_POST):

Array
(
    [tubo_0] => tubo1
    [largura_0] => largura1
    [tubo_1] => tubo2
    [largura_1] => largura2
)

I need to create a array containing the values of pipes and another array with the value of widths. Follow my failed attempt:

extract($_POST);
$qtdModelos = (count($_POST)) / 2;

for ($i=0; $i < $qtdModelos ; $i++) { 
    $arrayTubo[] = $tubo_.$i;
    $arrayLargura[] = $largura_.$i;
}

print_r($arrayTubo);
print_r($arrayLargura);

Upshot

Array
(
    [0] => 0
    [1] => 1
)
Array
(
    [0] => 0
    [1] => 1
)

Only the values of $i are being stored, that is, the variables are not being "concatenated" the way I would like them to be.

  • 1

    It wouldn’t be right for you to do it for example: for ($i=0; $i < $qtdModel ; $i++) { $arrayTubo[] = $tubo_[$i]; $arrayLargura[] = $largura_[$i]; } ???

  • Variables generated from the Extract ($_POST), in this example, the existing variables are $tubo_0, $tubo_1, $largura_0 and $largura_1. Need to store them within specific arrays for each property.

  • 2

    Why don’t you input.setAttribute("name",`tubo[${i}]['tubo']`) and input.setAttribute("name",`tubo[${i}]['largura']`)? You’ll already have one array with the data organized.

  • The reply of Rafael S. solved my problem, in this way ${tubo_.$i}.

2 answers

1


If I’m not mistaken you can do it:

${'tubo_' . $i}

Leaving your code that way:

extract($_POST);
$qtdModelos = (count($_POST)) / 2;

for ($i=0; $i < $qtdModelos ; $i++) { 
    $arrayTubo[] = ${tubo_.$i};
    $arrayLargura[] = ${largura_.$i};
}

print_r($arrayTubo);
print_r($arrayLargura);

In that link talks a little bit about it.

1

Instead of concatenating into the name, use the value of i as an index:

Tubo 1
Nome: <input type="text" name="tubo[0]">
Largura: <input type="text" name="largura[0]">

Tubo 2
Nome: <input type="text" name="tubo[1]">
Largura: <input type="text" name="largura[1]">

So when you send it to PHP, you can recover the different ones arrays with $_POST['tubo'] and $_POST['largura'].

Instead of doing it:

input.setAttribute("name",`tubo_${i}`)

You could do:

input.setAttribute("name",`tubo[${i}]`)

If the two fields are related to each other, you can better group your data in HTML. Instead of concatenating the value of i in the field name, use it as the index of array and set the columns in that index.

Tubo 1
Nome: <input type="text" name="tubo[0][nome]">
Largura: <input type="text" name="tubo[0][largura]">

Tubo 2
Nome: <input type="text" name="tubo[1][nome]">
Largura: <input type="text" name="tubo[1][largura]">

So when you send PHP, the value of $_POST will be something like:

Array
(
    [tubo] => Array
        (
            [0] => Array
                (
                    ['nome'] => tubo1
                    ['largura'] => 100
                )

            [1] => Array
                (
                    ['nome'] => tubo2
                    ['largura'] => 200
                )

        )

)

Therefore, to treat the values, it would be enough to go through this array and consume the information. A way would be:

foreach ($_POST['tubo'] as $tubo) {
    echo "O tubo {$tubo['nome']} possui largura {$tubo['largura']}";
}

See working on repl it.

  • Actually the data gets much more organized, but my goal is not to join tube with width, and rather separate these properties into different arrays in PHP, so I can treat them and insert them into the database in different columns.

  • @Gabrielzedine All you’d have to do is do array_column($_POST['tubo'], "largura"), or name according to fields, for example name="largura[0]" and directly search the value of $_POST['largura']

Browser other questions tagged

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