<?php
echo '<pre>';
error_reporting(E_ALL);
$pid = '129';
$families = array
(
"Griffin"=>array
(
"PTY"=>"Peter",
"STY"=>"X",
"QTY"=>"A|F"
),
"Quagmire"=>array
(
"NTY"=>"Glenn"
),
"Brown"=>array
(
"FTY"=>"Cleveland",
"OTY"=>"Q|G|T|Y",
"PTY"=>"Junior"
)
);
global $allid;
$allid = array();
function buildid($pid,$key,$val){
if (preg_match("/\|/",$val)){
$val = explode("|",$val);
foreach($val as $val1){
$id = $pid.'-'.$key.'-'.$val1;
$allid[] = $id;
}
}
}
print_r($allid);
foreach($families as $familieskey=>$familiesvalue){
foreach($familiesvalue as $skey=>$sval){
buildid($pid,$skey,$sval);
}
}
echo '</pre>';
?>
預期輸出爲上述代碼:PHP數組空的功能以外
情況1:
Array
(
[0] => 129-QTY-A
[1] => 129-QTY-F
)
Array
(
[0] => 129-OTY-Q
[1] => 129-OTY-G
[2] => 129-OTY-T
[3] => 129-OTY-Y
)
情況2:
Array
(
[0] => 129-QTY-A
[1] => 129-QTY-F
[2] => 129-OTY-Q
[3] => 129-OTY-G
[4] => 129-OTY-T
[5] => 129-OTY-Y
)
使用相同的代碼與不同的邏輯也可以重複嗎? – Neocortex