0

我试图在一行和可能的多行中回显 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 技巧可以做得更好吗?

4

2 回答 2

1

一定要改变这个:

if ($_PetsLoc = $rowA) {

对此:

if ($_PetsLoc == $rowA) {

基本上每次循环运行时,您都会重新分配变量,就好像它以前不存在一样。

于 2013-10-11T06:49:43.080 回答
1

试试下面的代码。您可以根据自己的用途对其进行修改:

<?php

   //$Display_Position1 = array ('Crocs' => $CrocsRow, 'Cat' => $CatRow, 'Rhino' => $RhinoRow, 'Wolf' => $WolfRow, 'Hyena' => $HyenaRow, 'Lion' => $LionRow, 'Tiger' => $TigerRow, 'Dingo' => $DingoRow, 'Bear' => $BearRow);
     $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  = 'Africa';  // to display column position, comes from another array and not included here
     $rowA  = 'CrocsRow';// to display row position, comes from another array and not included here


     foreach ($Display_Position as $_Pets => $_PetsLoc){    
       if ($_PetsLoc == $rowA) {
           echo $_Pets;
           //Attrib1
           array_key_exists($_Pets,$Assign_Attributes [$colA])?print('&nbsp;'.$Assign_Attributes ['Africa'][$_Pets]):print('&nbsp;blank'); // Africa is an eg. It actually is $colA
           //Attrib2        
           array_key_exists($_Pets,$Assign_Attributes [$colA])?print('&nbsp;'.$Assign_Attributes ['Africa'][$_Pets]):print('&nbsp;blank'); // Africa is an eg. It actually is $colA             
       }
       else{
           echo "<br />blank";
           //Attrib1
           array_key_exists($_Pets,$Assign_Attributes [$colA])?print('&nbsp;'.$Assign_Attributes ['Africa'][$_Pets]):print('&nbsp;blank'); // Africa is an eg. It actually is $colA        
           //Attrib2
           array_key_exists($_Pets,$Assign_Attributes [$colA])?print('&nbsp;'.$Assign_Attributes ['Africa'][$_Pets]):print('&nbsp;blank'); // Africa is an eg. It actually is $colA
       }
       echo "<br/>";    
     }
?>
于 2013-10-11T08:47:05.530 回答