这是我的问题的第2部分,following part 1。
我有两个阵列签名和指定。 Signatories和Designations数组具有分隔符"|"
,表示每个分区符合1,2个或更多签名者,取决于分隔符的数量。如果元素在分隔符和其他数组中有1,2或更多值,我想输出。它将产生另一个副本或克隆。像这样:
$signatories = array('Allan|Joshua|Ronald' , 'Robert|Mellisa' , 'Maria');
$designations = array('CEO|OIC' , 'CEO|COO|MANAGER' , 'MANAGER|OIC|COO');
我的预期输出:
***Array [0]***
Allan - CEO
Joshua - CEO
Ronald- CEO
Allan - OIC
Joshua - OIC
Ronald- OIC
***Array [1]***
Robert - CEO
Mellisa - CEO
Robert - COO
Mellisa - COO
Robert - MANAGER
Mellisa - MANAGER
***Array [2]***
Maria - MANAGER
Maria - OIC
Maria- COO
答案 0 :(得分:1)
一种功能性方法(如果从后面读取/接通后很容易理解):
$signatories = array('Allan|Joshua|Ronald', 'Robert|Mellisa', 'Maria');
$designations = array('CEO|OIC','CEO|COO','MANAGER|OIC|COO');
$result = array_map(function ($names, $titles) {
return call_user_func_array('array_merge', array_map(function ($name) use ($titles) {
return array_map(function ($title) use ($name) {
return $name . ' - ' . $title;
}, explode('|', $titles));
}, explode('|', $names)));
}, $signatories, $designations);
这会产生如下结果:
Array
(
[0] => Array
(
[0] => Allan - CEO
[1] => Allan - OIC
[2] => Joshua - CEO
[3] => Joshua - OIC
[4] => Ronald - CEO
[5] => Ronald - OIC
)
[1] => Array
(
[0] => Robert - CEO
[1] => Robert - COO
[2] => Mellisa - CEO
[3] => Mellisa - COO
)
[2] => Array
(
[0] => Maria - MANAGER
[1] => Maria - OIC
[2] => Maria - COO
)
)
此解决方案使用内置的PHP函数来解决问题:
array_map
一起。PHP没有数组展平功能,这就是:
call_user_func_array('array_merge', $input);
您还可以考虑使用RecursiveIterator from SPL
答案 1 :(得分:0)
<?php
$signatories = array('Allan|Joshua|Ronald' , 'Robert|Mellisa' , 'Maria');
$designations = array('CEO|OIC' , 'CEO|COO|MANAGER' , 'MANAGER|OIC|COO');
if (count($signatories) > count($designations))
Die("Sorry, but number of signatories is larger than number of designations, cannot continue.");
$result = [];
foreach ($signatories as $k => $v) {
$subresult = [];
$e = explode('|', $v);
$e2 = explode('|', $designations[$k]);
foreach ($e as $name) {
foreach ($e2 as $designation) {
$subresult[] = $name.' - '.$designation;
}
}
$result[] = $subresult;
}
echo "<pre>";
var_dump($result);
echo "</pre>";
答案 2 :(得分:0)
$signatories = array('Allan|Joshua|Ronald' , 'Robert|Mellisa' , 'Maria');
$designations = array('CEO|OIC' , 'CEO|COO|MANAGER' , 'MANAGER|OIC|COO');
foreach($signatories as $key=>$item){
$itemSet = explode('|', $item);
foreach($itemSet as $row){
$desSet = explode('|', $designations[$key]);
foreach($desSet as $pos){
echo $row." - ".$pos;
echo "<br/>";
}
}
}
答案 3 :(得分:0)
您需要两个循环,并且可以使用相同的索引。
$signatories = array('Allan|Joshua|Ronald', 'Robert|Mellisa', 'Maria');
$designations = array('CEO|OIC', 'CEO|COO|MANAGER', '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>';
}
}
}