簡體   English   中英

PHP在一行中回顯多個結果。 合並Foreach / Switch

[英]PHP to echo multiple results in one line. Merging Foreach/Switch

我正在嘗試在一行中以及可能在多行中回顯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技巧可以做得更好嗎?

確保更改此:

if ($_PetsLoc = $rowA) {

對此:

if ($_PetsLoc == $rowA) {

基本上,每次循環運行時,您都在重新分配該變量,就好像它以前不存在一樣。

試試下面的代碼。 您可以根據自己的使用進行修改:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM