Php 在For循环中获得意外结果 我有这个功能。。。 但出乎意料地

Php 在For循环中获得意外结果 我有这个功能。。。 但出乎意料地,php,for-loop,Php,For Loop,我调用这个函数就像 $groupNameArry = $this->getGroupName(MAX_ALLOWED_PARTICIPANT); 其中允许的最大参与者值为100。怎么了?请帮忙 在您的代码中$i=0在每个循环中 这解决了你的问题 /** * Get group name * * @param int $no_of_participant * @return string */ public static function getGroupName(int $

我调用这个函数就像

$groupNameArry = $this->getGroupName(MAX_ALLOWED_PARTICIPANT);

其中允许的最大参与者值为100。怎么了?请帮忙

在您的代码中
$i=0
在每个循环中

这解决了你的问题

 /**
 * Get group name
 * 
 * @param int $no_of_participant
 * @return string
 */
public static function getGroupName(int $no_of_participant)
{
    $groupNameArry = array();
    $groupNumber = 0;

    $letters = range('A', 'Z');

    foreach ($letters as $letter) {

        for ($i = 0; $i <= $no_of_participant / 2; $i++) {
            if ($i == 0) {
                $groupNameArry[$groupNumber] = $letter;
            } else {
                $groupNameArry[$groupNumber] = $letter . $i;
            }
            $groupNumber++;
        }
    }

    return $groupNameArry;
}
/**
*获取组名
* 
*@param int$参与者的编号
*@返回字符串
*/
公共静态函数getGroupName(int$no_of_participant)
{
$groupNameArry=array();
$groupNumber=0;
$letters=范围('A','Z');
foreach($字母作为$字母){

对于($i=0;$i在您的代码中,
$i=0
在每个循环中

这解决了你的问题

 /**
 * Get group name
 * 
 * @param int $no_of_participant
 * @return string
 */
public static function getGroupName(int $no_of_participant)
{
    $groupNameArry = array();
    $groupNumber = 0;

    $letters = range('A', 'Z');

    foreach ($letters as $letter) {

        for ($i = 0; $i <= $no_of_participant / 2; $i++) {
            if ($i == 0) {
                $groupNameArry[$groupNumber] = $letter;
            } else {
                $groupNameArry[$groupNumber] = $letter . $i;
            }
            $groupNumber++;
        }
    }

    return $groupNameArry;
}
/**
*获取组名
* 
*@param int$参与者的编号
*@返回字符串
*/
公共静态函数getGroupName(int$no_of_participant)
{
$groupNameArry=array();
$groupNumber=0;
$letters=范围('A','Z');
foreach($字母作为$字母){

for($i=0;$i
是一个字符串


$letter
变为
'Z'
时,条件
$letter
$letter
是一个字符串


$letter
变为
'Z'
时,条件
$letter您的代码更像应该返回A,B1,C2,D3try
范围('A','Z');
字母循环数组。您的代码更像应该返回A,B1,C2,D3try
范围('A','Z');
字母循环数组。
$groupNameArry = $this->getGroupName(MAX_ALLOWED_PARTICIPANT);
 /**
 * Get group name
 * 
 * @param int $no_of_participant
 * @return string
 */
public static function getGroupName(int $no_of_participant)
{
    $groupNameArry = array();
    $groupNumber = 0;

    $letters = range('A', 'Z');

    foreach ($letters as $letter) {

        for ($i = 0; $i <= $no_of_participant / 2; $i++) {
            if ($i == 0) {
                $groupNameArry[$groupNumber] = $letter;
            } else {
                $groupNameArry[$groupNumber] = $letter . $i;
            }
            $groupNumber++;
        }
    }

    return $groupNameArry;
}
foreach (range('A', 'Z') as $letter) {
    echo($letter);
}
echo("\nDone.");
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Done.