0

So i have 2 POSTs coming as arrays:

$ post1 Array ( [0] => Tipul1 [1] => tipul2 [2] => tipul3 )

$ post2 Array ( [0] => cant1 [1] => cant2 [2] => cant3 )

What I want to achieve is send these in a db(the query wont be a problem) in this format(the format is a problem and the way I concatenate the values):

Tipul 1 - cant1 | Tipul 2 - cant2 | Tipul 3 - cant3

So , how can I combine those arrays and add the - between each value ?

Using

foreach ($tip as $tipq) {

    foreach ($cantitate as $cantitateq) {

        echo $tipq.''.$cantitateq. "<br>";

    }
}

I would get this(it makes sense): Tipul1cant1 Tipul1cant2 Tipul1cant3 tipul2cant1 tipul2cant2 tipul2cant3 tipul3cant1 tipul3cant2 tipul3cant3

an4rei
  • 57
  • 1
  • 7
  • `$arr = array_combine($post1,$post2)` gives `array('Tipul1'=>'cant1', ... );` – JustOnUnderMillions Mar 31 '17 at 13:43
  • As long you have not show any try outs i will give only hints: You have to reformat `Tipul1` to `Tipul 1`, ... and combine&collect all values with something like `$arr[] = "$value1 - $value2";`, collect that in an array, then that array can be `implode(' | ',$array);` then you have what you want – JustOnUnderMillions Mar 31 '17 at 13:45

1 Answers1

1

You have to iterate over the elements to combine them. Take a look at this simple three step example:

<?php
$input = array_combine(
    ['Tipul1', 'Tipul2', 'Tipul3'],
    ['cant1', 'cant2', 'cant3']
);
$output = [];
array_walk($input, function($val, $key) use (&$output) {
    $output[] = $key . ' - ' . $val;
});
var_dump(implode(' | ', $output));

The output obviously is:

string(48) "Tipul1 - cant1 | Tipul2 - cant2 | Tipul3 - cant3"

arkascha
  • 41,620
  • 7
  • 58
  • 90