我有2 array datas
,("a","b","c")
和("x","y","z")
,如何混合它们并将结果放在(ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)
? (每个$array1 + $array2
组合成一个新词)。
$array1 = array("a","b","c");
foreach($array1 as $data1){
}
$array2 = array("x","y","z");
foreach($array2 as $data2){
}
//$output = '(ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)';
答案 0 :(得分:8)
$array = array();
$array1 = array("a","b","c");
$array2 = array("x","y","z");
foreach($array1 as $data1){
foreach($array2 as $data2){
$array[] = '('.$data1.$data2.')';
}
}
echo implode('|', $array);
答案 1 :(得分:7)
<?php
$array1 = array("a","b","c");
$array2 = array("x","y","z");
$array3 = array();
foreach($array1 as $data1){
foreach($array2 as $data2){
$array3[] = "($data1$data2)";
}
}
echo implode('|', $array3);
// (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)
答案 2 :(得分:6)
您需要使用嵌套循环:
$items = array();
foreach($array1 as $a)
{
foreach($array2 as $b)
{
$items[] = '(' . $a . $b . ')';
}
}
echo implode('|', $items); // (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)