2013-10-11 85 views
0

我想在一行中回显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技巧可以做得更好?

+0

您真的想在if条件中将'$ _PetsLoc'赋值给'$ rowA'吗? – Spell

+0

你的意思是输出应该显示如:“Crocs Strong Healthy”? –

+0

@MahavirM​​unot没错。它也可以是鳄鱼强壮的Strong2或Crocs空的空或空什么都没有显示空的空空 – Mike

回答

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/>";  
    } 
?> 
1

一定要改变这一点:

if ($_PetsLoc = $rowA) { 

这样:

if ($_PetsLoc == $rowA) { 

基本上回路运行时,你重新分配变量中的每个时间,如果它没有存在过。

+0

感谢和注意。它实际上是我的代码中的“==”。代码更多的行和数组。 $ rowA也是一个简化的表示。它从数组中提取数据,然后从数组中取出数据,并获得很少的模数。我不确定是否有更好的方式来表达这一点。可以将Block 1和Block 2组合起来吗? – Mike

相关问题