我试图在一行和可能的多行中回显 3 个信息。所有这 3 个信息都来自不同的数组或重叠的数组。
最终想要的结果:
Pet1(如果有) Attrib1(如果有) Attrib2(如果有)
Pet2(如果有) Attrib1(如果有) Attrib2(如果有)
Pet3(如果有) Attrib1(如果有) Attrib2(如果有)
复杂性是由于 Pets、Attrib1 和 Attrib2 高度可变且取决于用户输入的位置而发生的。它们中的任何一个都可能出现或完全无效。
我设法使用 Switch 将 Pet 和 Attrib1 拉到一起。但是为了呼应 Attrib2,我应该使用另一个 Switch(下面的块 2)。以及如何将它们全部合并为一行和一列(即 Pet Attrib1 Attrib2)?
// This specifies which row it should display
// Each of the following $xxxRow contains information about the Row position in another array
// Didn't include the Row array here to simplify my question
$Display_Position = array (
'Crocs' => $CrocsRow, 'Cat' => $CatRow, 'Rhino' => $RhinoRow, 'Wolf' => $WolfRow, 'Hyena' => $HyenaRow, 'Lion' => $LionRow, 'Tiger' => $TigerRow, 'Dingo' => $DingoRow, 'Bear' => $BearRow);
// ------------------
$Assign_Attributes = array (
'US'=>
array ('Wolf'=>'Strong', 'Dingo'=>'Healthy', 'Tiger'=>'Fast', 'Cat'=>'Timid'),
'Africa'=>
array ('Tiger'=>'Strong', 'Hyena'=>'Healthy', 'Crocs'=>'Fast', 'Rhino'=>'Timid'),
'Asia'=>
array ('Cat'=>'Strong', 'Wolf'=>'Healthy', 'Crocs'=>'Fast', 'Hynena'=>'Timid'),
'Ocenia'=>
array ('Bear'=>'Strong', 'Crocs'=>'Healthy', 'Cat'=>'Fast', 'Dingo'=>'Timid'),
);
// ------------------
$colA // to display column position, comes from another array and not included here
$rowA // to display row position, comes from another array and not included here
// ------------------
// BLOCK 1: $_Pets with its assigned attributes
// Success. Managed to display $_Pets with the $Assign_Attributes
// This will display either "Tiger (if any)" or "Tiger (if any) Strong (if any)" in the same row/column
foreach ($Display_Position as $_Pets => $_PetsLoc){
if ($_PetsLoc = $rowA) {
$Attrib = $Assign_Attributes ['Africa'][$_Pets]; // Africa is an eg. It actually is $colA
switch ($Attrib) {
case 'Strong':
echo $_Pets.'Strong<br/ >'; break;
case 'Healthy':
echo $_Pets.'Healthy<br/ >'; break;
case 'Fast':
echo $_Pets.'Fast<br/ >'; break;
case 'Timid':
echo $_Pets.'Timid<br/ >'; break;
default:
echo $_Pets.'<br/ >';
break;
}
}
}
// ------------------
// BLOCK 2: Attributes 2
// Block 2 should desirable be merged with BLOCK 1.
// Example: This will eventually display either "Tiger (if any)" or "Tiger Timid (if any)"
// Right now, i'm using CSS to work around for the display of Block 2, which is messy
foreach ($Display_Position as $_Pets => $_PetsLoc){
if ($_PetsLoc = $rowA) {
$Attrib2 = $Assign_Attributes ['Africa'][$_Pets]; // Africa is an eg. It actually is $colA
switch ($Attrib2) {
case 'Strong':
echo 'Strong2<br/ >';break;
case 'Healthy':
echo 'Healthy2<br/ >';break;
case 'Fast':
echo 'Fast2<br/ >';break;
case 'Timid':
echo 'Timid2<br/ >';break;
}
}
}
这太复杂了吗?这对我的水平来说绝对是太具有挑战性了。当有多个 Switch 时,还有其他 PHP 技巧可以做得更好吗?