【问题标题】:PHP to echo multiple results in one line. Merging Foreach/SwitchPHP 在一行中回显多个结果。合并 Foreach/Switch
【发布时间】:2013-10-11 05:36:50
【问题描述】:

我正在尝试在一行中回显 3 条信息,并可能在多行中回显。所有这 3 个信息都来自不同的数组或重叠的数组。

最终想要的结果:

宠物1(如果有)属性1(如果有)属性2(如果有)

宠物2(如果有)属性1(如果有)属性2(如果有)

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 技巧可以做得更好吗?

【问题讨论】:

  • 你真的想在 if 条件下将$_PetsLoc 分配给$rowA 吗?
  • 您的意思是输出应该显示类似的内容,例如:“Crocs Strong Healthy”?
  • @MahavirM​​unot 没错。也可以是 Crocs Strong Strong2 或 Crocs empty empty 或什么都不显示 empty empty empty
  • @Mike 你能添加 $colA,$rowA 数组吗
  • @MahavirM​​unot 可以做到。但是 $colA 和 $rowA 实际上从数组读取到另一个和另一个......如果我在这里包含所有它们会不会很乱?

标签: php arrays echo


【解决方案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/>";    
     }
?>

【讨论】:

    【解决方案2】:

    请务必更改:

    if ($_PetsLoc = $rowA) {
    

    到这里:

    if ($_PetsLoc == $rowA) {
    

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

    【讨论】:

    • 感谢并注明。它实际上是我的代码中的“==”。代码的行数和数组要多得多。 $rowA 也是一个简化的表示。它从数组中提取数据,然后从数组和少量模数中提取数据。我不确定是否有更好的方式来表达这一点。可以将 Block 1 和 Block 2 合并在一起吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多