2017-03-31 104 views
0

,所以我有2个职位来为数组:结合两个数组

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

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

我想实现的是在DB发送这些(查询不会是一个这种格式的问题)(格式是一个问题,我的方式串联值):

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

所以,我怎么能结合这些阵列,并添加每个值之间有?

使用

foreach ($tip as $tipq) { 

    foreach ($cantitate as $cantitateq) { 

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

    } 
} 

我会得到这个(这是有道理的): Tipul1cant1 Tipul1cant2 Tipul1cant3 tipul2cant1 tipul2cant2 tipul2cant3 tipul3cant1 tipul3cant2 tipul3cant3

+0

'$ arr = array_combine($ post1,$ post2)'给出'array('Tipul1'=>'cant1',...);' – JustOnUnderMillions

+0

只要你没有显示任何尝试,我只会给你提示:你有将'Tipul1'重新格式化为'Tipul 1',...并将所有值与'$ arr [] =“$ value1 - $ value2”;'合并并收集,一个数组,那么该数组可以是'implode('| ',$ array);'那么你有你想要的 – JustOnUnderMillions

回答

1

你遍历元素来组合它们。看看这个简单的三步例如:

<?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)); 

输出显然是:

字符串(48) “Tipul1 - CANT1 | Tipul2 - cant2 | Tipul3 - cant3”