【问题标题】:How do I break elements for <TR> in a nested foreach?如何在嵌套的 foreach 中打破 <TR> 的元素?
【发布时间】:2022-10-20 18:39:23
【问题描述】:

我的目标是打破每种奖励类型的货币数组,如图所示:

但是,我得到的是这个输出(它列出了每个 TR 行的每种甲板类型的所有货币奖励):

我当前的代码是这样的(我为混乱的代码道歉):

// Sample array values
$choiceVal = "2, 3, 4";
$randomVal = "3, 4, 5";
$moneyVal = "0 | 20, 2 | 25, 3 | 30";
$tcgCurr = "charm points.png, lollipop.png";

// Run array of rewards
$choiceDeck = explode(", ", $choiceVal);
$randomDeck = explode(", ", $randomVal);
$moneyDeck = explode(", ", $moneyVal);

// Explode currency bombs
foreach( $moneyDeck as $ext => $value )
{
    $curValue = explode(' | ', $moneyDeck[$ext]);
    $curName = explode(', ', $tcgCurr);

    foreach( $curValue as $key => $value )
    {
        $tn = substr_replace($curName[$key],"",-4);
        if( $curValue[$key] > 1 )
        {
            $var = substr($tn, -1);
            if( $var == "y" )
            {
                $tn = substr_replace($tn,"ies",-1);
            }
            else if( $var == "o" )
            {
                $tn = substr_replace($tn,"oes",-1);
            }
            else
            {
                $tn = $tn.'s';
            }
        }

        else
        {
            $tn = $tn;
        }

        if( $curValue[$key] == 0 ) {}
        else
        {
            $arrayCur[] = $curValue[$key].' '.$tn.', ';
        }
    }
}
// Fix all bombs after explosions
$arrayCur = implode(" ", $arrayCur);
$arrayCur = substr_replace($arrayCur,"",-2);

// Display table of rewards
echo '<table width="100%" cellspacing="3" class="table table-striped">
<thead>
    <tr>
        <th scope="row" width="40%" align="center">Mastery</th>
        <th scope="row" width="60%" align="center">Rewards</th>
    </tr>
</thead>

<tbody>';

foreach( $randomDeck as $disp => $value )
{
    // Add tr lines for each deck mastery type
    if( $disp == 0 ) { $mast = 'Regular'; }
    elseif( $disp == 1 ) { $mast = 'Special'; }
    elseif( $disp == 2 ) { $mast = 'Rare'; }
            
    echo '<tr>
        <td align="center">'.$mast.' Decks</td>
        <td align="center">'.$choiceDeck[$disp].' choice, '.$randomDeck[$disp].' random, '.$arrayCur.'</td>
    </tr>';
}

echo '</tbody>
</table>';

非常感谢任何解决方法或简单的方法和帮助实现这一目标!

【问题讨论】:

  • 问题显然是变量$arrayCur 的内容。它不包含您可能的想法。
  • @arkascha 你好~!是的。我很清楚是$arrayCur 导致了这个问题。这是我需要帮助打破的。谢谢 :)

标签: php


【解决方案1】:

在休息了几天后,我终于能够弄清楚这一点。初始代码有一堆foreach,所以我尝试通过合并货币 foreach 和 TR 行 foreach 来解决问题。 :)

// Sample array values
$choiceVal = "2, 3, 4";
$randomVal = "3, 4, 5";
$moneyVal = "0 | 20, 2 | 25, 3 | 30";
$tcgCurr = "charm points.png, lollipop.png";

// Run array of rewards
$choiceDeck = explode(", ", $choiceVal);
$randomDeck = explode(", ", $randomVal);
$moneyDeck = explode(", ", $moneyVal);

echo '<table width="100%" cellspacing="3" class="table table-striped">
    <thead>
    <tr>
        <th scope="row" width="40%" align="center">Mastery</th>
        <th scope="row" width="60%" align="center">Rewards</th>
    </tr>
    </thead>

    <tbody>';

    foreach( $moneyDeck as $disp => $value )
    {
        $curValue = explode(' | ', $moneyDeck[$disp]);
        $curName = explode(', ', $settings->getValue( 'tcg_currency' ));
        $arrayCur = array();
        foreach( $curValue as $key => $value )
        {
            $tn = substr_replace($curName[$key],"",-4);
            if( $curValue[$key] > 1 )
            {
                $var = substr($tn, -1);
                if( $var == "y" )
                {
                    $tn = substr_replace($tn,"ies",-1);
                }
                else if( $var == "o" )
                {
                    $tn = substr_replace($tn,"oes",-1);
                }
                else
                {
                    $tn = $tn.'s';
                }
            }

            else
            {
                $tn = $tn;
            }

            if( $curValue[$key] == 0 ) {}
            else
            {
                $arrayCur[] = $curValue[$key].' '.$tn.', ';
            }
        }
        $arrayCur = implode(" ", $arrayCur);
        $arrayCur = substr_replace($arrayCur,"",-2);

        // Add tr lines for each deck mastery type
        if( $disp == 0 ) { $mast = 'Regular'; }
        elseif( $disp == 1 ) { $mast = 'Special'; }
        elseif( $disp == 2 ) { $mast = 'Rare'; }

        echo '<tr>
            <td align="center">'.$mast.' Decks</td>
            <td align="center">'.$choiceDeck[$disp].' choice, '.$randomDeck[$disp].' random, '.$arrayCur.'</td>
        </tr>';
    }

    echo '</tbody>
</table>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多