Join two array

Asked

Viewed 58 times

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?

  • Makes a for or foreach and create a new array is the good way to do it ... but, a question why unite, if you need to do what ?:

  • 4

    The way you want it to stay doesn’t give, you can’t repeat keys in the array.

  • 1

    Gustavo make a tour you have open questions on the site, take a look at how to act here ...

  • @Viniciusgabriel will always have the same size, is made a pre-check.

  • 1

    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.

  • @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.

  • 1

    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.

  • @bfavaretto understand, Thanks for the honesty heheh.

  • 1

    @Augustovasques Sorry, it is not my intention to offend the community, I will be reviewing my way of acting! thanks for the tip!

  • 1

    @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?

  • @Augustovasques No doubt it’s interesting, if you can lucidar, I’ll be grateful!

Show 7 more comments

1 answer

2


As explained in the comments the desired array format cannot be reached because in PHP an associative array cannot have repeated keys. Attempting to add a repeated key results in overwriting of the last inserted value.

As specified in the comments, both array will have the same length possibility is to rearrange the data and an array where each element has a id coming from the array $produtos and the amount coming from the array $quantidade:

<?php

$produto = ["Toras", "Toras", "Toras"];

$quantidade = ["0.7600 M3", "29.5700 M3", "29.5700 M3"];

$romaneio = [];

foreach(range(0, count ($produto) - 1) as $i){
  $romaneio[] = ["id"=> $produto[$i], "quantidade" =>$quantidade[$i]];

}


print_r($romaneio);

Which results in the following array:

Array
(
    [0] => Array
        (
            [id] => Toras
            [quantidade] => 0.7600 M3
        )

    [1] => Array
        (
            [id] => Toras
            [quantidade] => 29.5700 M3
        )

    [2] => Array
        (
            [id] => Toras
            [quantidade] => 29.5700 M3
        )

)

Code working in Repl.it: https://repl.it/repls/NotableCapitalDatalog

  • 1

    It worked perfectly, even I will use in other parts of my code, really helped me in this charade!

Browser other questions tagged

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