foreach循环里面while循环returns只有一个结果
Foreach loop inside while loop returns only one result
正如标题所说,while 循环中的 foreach 循环 returns 只有最后一个数组。
当前输出:
紫水晶 04、彩虹 07、海星 14、+3 金
期望输出:
紫水晶 04,彩虹 07,海星 14,+2 瓶,+1 糖果,+3 金
所以下面的代码只returns货币数组中的黄金数组。
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 吗?
正如标题所说,while 循环中的 foreach 循环 returns 只有最后一个数组。
当前输出: 紫水晶 04、彩虹 07、海星 14、+3 金
期望输出: 紫水晶 04,彩虹 07,海星 14,+2 瓶,+1 糖果,+3 金
所以下面的代码只returns货币数组中的黄金数组。
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 吗?