我有两个数组 signatories 和名称。名称数组具有分隔符"|"
,表示1个签名者有2个名称。如果元素在分隔符中有2个值而在其他数组中有1个,我想输出。它将产生另一个副本或克隆。像这样。
$signatories = array('Allan','Robert','Maria');
$designations = array('CEO','CEO|COO','MANAGER|OIC|COO');
我的预期输出:
Allan - CEO
Robert - CEO
Robert - COO
Maria - MANAGER
Maria - OIC
Maria- COO
答案 0 :(得分:2)
您需要两个循环,并且可以使用相同的索引。
$signatories = array('Allan|Joshua|Ronald', 'Robert|Mellisa', 'Maria');
$designations = array('CEO','CEO|COO','MANAGER|OIC|COO');
$cs = count($signatories);
for ($i=0; $i<$cs; $i++) {
$desigs = explode('|', $designations[$i]);
$signas = explode('|', $signatories[$i]);
foreach ($desigs as &$desig) {
foreach ($signas as &$signa) {
echo $signa.' - '.$desig.'<br>';
}
}
}
答案 1 :(得分:0)
使用foreach循环和explode()函数,您可以将两个数组合并到一个数组中。
$signatories = array('Allan','Robert','Maria');
$designations = array('CEO','CEO|COO','MANAGER|OIC|COO');
$combinedArray = array();
foreach($signatories as $key => $value){
$combinedArray[] = array('name' => $value, 'designations' => explode("|",$designations[$key]));
}
print_r($combinedArray);die;
$ combinedArray数组同时包含名称和数组。
此阵列的输出 -
Array
(
[0] => Array
(
[name] => Allan
[designations] => Array
(
[0] => CEO
)
)
[1] => Array
(
[name] => Robert
[designations] => Array
(
[0] => CEO
[1] => COO
)
)
[2] => Array
(
[name] => Maria
[designations] => Array
(
[0] => MANAGER
[1] => OIC
[2] => COO
)
)
)
答案 2 :(得分:0)
有点乱,但有一个循环。
<?php
$signatories = array('Allan','Robert','Maria');
$designations = array('CEO','CEO|COO','MANAGER|OIC|COO');
$i = 0;
foreach($designations as $designation) {
$newArr = explode('|',$designation);
echo $signatories[$i].' - '.implode('<br>'.$signatories[$i].' - ',$newArr).'<br><br>';
$i++;
}
答案 3 :(得分:0)
另一种方式,
$signatories = array('Allan','Robert','Maria');
$designations = array('CEO','CEO|COO','MANAGER|OIC|COO');
foreach($signatories as $key=>$val)
{
$exp = explode('|',$designations[$key]);
foreach($exp as $dsg)
{
echo $val.' - '.$dsg;
echo "<br>";
}
echo "<br>";
}
,你的输出将是,
Allan - CEO
Robert - CEO
Robert - COO
Maria - MANAGER
Maria - OIC
Maria - COO