1
I have a form with fields products and amount which are imported by invoice:
<div class="row">
<div class="form-group col-md-9">
<label for="produto_nfe">Produto</label>
<input type="text" value="" class="form-control" id="produto_nfe" name="produto_nfe[]" aria-describedby="" placeholder="predominância">
<div class="produtos"></div>
</div>
<div class="form-group col-md-3">
<label for="quantidade_nfe">Qtd</label>
<input type="text" value="" class="form-control" id="quantidade_nfe" name="quantidade_nfe[]" aria-describedby="" placeholder="X">
<div class="quantidade"></div>
</div>
</div>
They are passed via ajax with $(form).serialize();
and as they are various fields, step them as a array for php.
Use the following php code to receive:
$produtos = $_POST['produto_nfe'];
$quantidades = $_POST['quantidade_nfe'];
When I give a var_dump returns the following result:
Products:
array(3)
{
[0]=> string(5) "Toras"
[1]=> string(5) "Toras"
[2]=> string(5) "Toras"
}
Quantities:
array(3)
{
[0]=> string(9) "0.7600 M3"
[1]=> string(10) "29.5700 M3"
[2]=> string(10) "29.5700 M3"
}
I need these fields to be related in a new array so that I can add in stock, I would like it to be in this way:
array(3)
{
NOME QUANTIDADE
[Toras]=> string(5) "0.7600 M3"
[Toras]=> string(5) "29.5700 M3"
[Toras]=> string(5) "29.5700 M3"
}
I’ve tried to array_combine
:
$produtos = $_POST['produto_nfe'];
$quantidades = $_POST['quantidade_nfe'];
var_dump(array_combine($produtos, $quantidades));
But only returns one result:
array(1)
{
["Toras"]=> string(10) "29.5700 M3"
}
How could it be done to keep all quantity-related products?
Will the two arrays always have the right size? And always the quantity will follow the order of what informs the products?
– Vinicius Gabriel
Makes a
for
orforeach
and create a newarray
is the good way to do it ... but, a question why unite, if you need to do what ?:– novic
The way you want it to stay doesn’t give, you can’t repeat keys in the array.
– bfavaretto
Gustavo make a tour you have open questions on the site, take a look at how to act here ...
– novic
@Viniciusgabriel will always have the same size, is made a pre-check.
– Gustavo Lucksik
It’s hard to give you an answer because I’ve been looking at your question history and you don’t vote or accept the answers to your questions.
– Augusto Vasques
@Virgilionovic I need to put in the database each product, so if I have in the patio "Eucalyptus logs" and have "30 MB" when launching the invoice it will relate "invoice number" "product" "quantity". as you gave the tip to use a loop, I tried in every way and confess that I walked in circles and nothing.
– Gustavo Lucksik
Without resolving the @bfavaretto comment there is no way to reply. What you ask is impossible to do, since an associative array cannot have equal keys.
– Woss
@bfavaretto understand, Thanks for the honesty heheh.
– Gustavo Lucksik
@Augustovasques Sorry, it is not my intention to offend the community, I will be reviewing my way of acting! thanks for the tip!
– Gustavo Lucksik
@Gustavolucksik, as bfavaretto already explained has no way to leave the way you want due to the key restriction of the associative array. But you can leave the data in other formats. It is interesting to you an answer that the array is in another format than what you specified in the question?
– Augusto Vasques
@Augustovasques No doubt it’s interesting, if you can lucidar, I’ll be grateful!
– Gustavo Lucksik