正如标题所说,while 循环内的 foreach 循环只返回最后一个数组。
当前输出:紫水晶04、彩虹07、海星14、+3金
期望输出:紫水晶04、彩虹07、海星14、+2小瓶、+1糖果、+3金
所以下面的代码只从货币数组中返回黄金数组。
while( $row = mysqli_fetch_assoc($show1) ) {
// Example of $row['log_rewards'] output from the database log_rewards column:
$row['log_rewards'] = "amethyst04, rainbow07, starfish14, vial, vial, candy, gold, gold, gold";
// List of available currencies
$currencyArr = "vial, candy, gold";
$rewards = explode(', ', $row['log_rewards']);
$curName = explode(', ', $currencyArr);
// Declare empty strings
$txtString = '';
$curString = '';
// Display cards for each reward if NOT a currency
foreach( $rewards as $r ) {
if( !in_array($r, $currencyNames) ) {
$txtString .= $r.', ';
}
}
// Get count of how many of each reward is present
$values = array_count_values($rewards);
// Display currencies that are in rewards and quantity only if exists in rewards
// This foreach loop only shows the last currency from the $rewards array
foreach( $curName as $cn ) {
if( array_key_exists($cn, $values) ) {
$curString .= ', +'.$values[$cn].' '.$cn;
}
}
// Display text of rewarded cards
$txtString = substr_replace($txtString,"",-2);
// Print out both cards and currencies
echo '<b>'.$row['log_title'].':</b> '.$txtString.' '.$curString;
}
我不确定是否应该进一步解释这一点,因为我已经提到了当前和所需的输出以及问题。^^; 提前谢谢了!
if( !in_array($r, $currencyNames) ) {
因为没有名为 $currencyNames 的变量。不应该是 $curName 吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句