Foreach Array incrementing another array

Asked

Viewed 3,023 times

6

with a doubt in performing a foreach and incrementing another helper to perform an Insert in the bank, see only:

When I send a form via post I have the following array as a result:

array (size=2)
  'quantidade' => 
    array (size=8)
      0 => string '10' (length=2)
      1 => string '11' (length=2)
      2 => string '15' (length=0)
      3 => string '22' (length=0)
      4 => string '10' (length=0)
      5 => string '9' (length=0)
      6 => string '0' (length=0)
      7 => string '35' (length=0)
  'produto' => 
    array (size=8)
      0 => string '11' (length=1)
      1 => string '18' (length=1)
      2 => string '19' (length=1)
      3 => string '21' (length=1)
      4 => string '22' (length=2)
      5 => string '25' (length=2)
      6 => string '29' (length=2)
      7 => string '22' (length=2)

How I’m using Codeigniter to get the Form data, using the following structure:

foreach ($this->input->post() as $chave => $valor) {
  //o array aux aqui
}

Any idea how to generate this result by taking the first value of each element and inserting it into an auxiliary array:

Kind of:

$array_aux[0] = array('quantidade' => 10, 'produto' => 11)
...
$array_aux[7] = array('quantidade' => 35, 'produto' => 22)
  • Wouldn’t it be better to make one normal and 'control' the two indices by $i ?

  • Hello @rray, so I’m using codeigniter and picking up these variables through $this->input->post(), so I thought about foreach. You could help me use the for that?

  • This auxiliary array you will send to the bank? what is its structure?

  • yes, I will send it to the bank. The DB structure is id, quantity. That in the example I passed the id is equal to product key

3 answers

6

  • Opa @Bacco thanks for the help with just one line. In the case to using Codeigniter, and I receive the data through $this->input->post(), so I used the foreach, I would know help me in this case?

  • If $this->input->post() returns array, only make $array = $this->input->post() before the above line, and if it returns object, only exchange $array['quantity'] for ->quantity in object, and the same in product. Post a print_r of your $this->input->post() in the Pastebin and the link here in the comments for me to see. Anyway, you can use the same logic.

  • Okay my dear, I will perform the post there on Pastebin, anyway I have already put in the description of the question what you asked. No more thanks for the help again

  • However, it doesn’t need a loop. The line above solves it. But send the Pastebin link here that I see fits.

  • But don’t forget, put a print_r even, because you can see the whole structure.

  • Then, analyzing the print we are in the correct path, I just could not mount the keys as in the example $array_aux[0] = array('quantity' => 10, 'product' => 11), since this will be inserted in the database. I’ll take a look here at what you sent me

  • @tkmattos $array = $this->input->post(); $array_aux = array_combine( $array['quantidade'], $array ['produto'] );

  • Yes @Bacco understood this logic, trying is to put here the idea of creating the arrays to insert in the bank, since each pair of product and quantity will be an input in the bank, so I will need to put name in the keys

  • @tkmattos I’m going by what you put in the question, it’s hard to imagine how you’re doing the insertion. As required, open a question with the insertion code itself.

  • Calm my dear, from here already helped a lot, I will try to modify based on what you sent me, and put the answer to the question to help all tbm. Thanks again for the help of always!!!

Show 6 more comments

5


$arrayForm = array();

foreach($array['quantidade'] as $key => $val){
    $arrayForm[] = array(
                   'quantidade' => $val, 
                   'produto'    => $array['produtos'][$key]
    ); 
}

echo '<pre>';
print_r($arrayForm);
echo '</pre>';
  • perfect your logica @Gumball came out exactly as needed, and I can still add new keys. The scheme is to use the $key variable to get the correct key positions?

  • 1

    Exactly, takes the same key of the two arrays.

3

If you are going to mount an array and do an Insert via querybuilder CI, normally the key names should be the column names in the database, in which case you can use the function array_map() to match the values.

The anonymous function returns an array that will have as its value the first (and all) item of $a1['quantidade'] and $a2['produto'].

$a1 = array('quantidade' => array(10,11,15,22,10,9,0,35));
$a2 =  array('produto' => array(11,18,19,21,22,25,29,22));

$r = array_map(function($q, $p){ return ['quantidade' => $q, 'produto' => $p] ;}, $a1['quantidade'], $a2['produto']); 

echo "<pre>";
print_r($r);

Exit:

Array
(
    [0] => Array
        (
            [quantidade] => 10
            [produto] => 11
        )

    [1] => Array
        (
            [quantidade] => 11
            [produto] => 18
        )

    [2] => Array
        (
            [quantidade] => 15
            [produto] => 19
        )

    [3] => Array
        (
            [quantidade] => 22
            [produto] => 21
        )

    [4] => Array
        (
            [quantidade] => 10
            [produto] => 22
        )

    [5] => Array
        (
            [quantidade] => 9
            [produto] => 25
        )

    [6] => Array
        (
            [quantidade] => 0
            [produto] => 29
        )

    [7] => Array
        (
            [quantidade] => 35
            [produto] => 22
        )

)

Browser other questions tagged

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