我想在一行中回显3个信息,并且可能有多行。所有这些3个信息都来自不同的阵列或重叠阵列。PHP在一行中回显多个结果。合并Foreach /开关
最终期望的结果:
PET1(如果有的话)Attrib1(如果有的话)Attrib2(如果有的话)
PET2(如果有的话)Attrib1(如果有的话)Attrib2(如果有的话)
PET3(如果有的话)Attrib1(如果有的话)Attrib2(如果有的话)
并发症发生的位置的结果,其中所述宠物,Attrib1和Attrib2是高度杂物并且取决于用户的输入。它们中的任何一个可能完全显示或为空。
我设法使用开关将Pet和Attrib1拉到一起。但是为了回应Attrib2,我应该使用另一个开关(下面的方框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技巧可以做得更好?
您真的想在if条件中将'$ _PetsLoc'赋值给'$ rowA'吗? – Spell
你的意思是输出应该显示如:“Crocs Strong Healthy”? –
@MahavirMunot没错。它也可以是鳄鱼强壮的Strong2或Crocs空的空或空什么都没有显示空的空空 – Mike