doulian8554 2016-07-21 05:18
浏览 14
已采纳

请帮我在PHP中使用for循环

I want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track of what number person im on. -- im using PHP

<?php
$name_array = array('pon', 'zi', 'pol', 'et');

function name_person($name, $number) {
    echo $name . ' is person #' . $number . ' ';
}

for ($i = 0; $i <= 4; $i++) {
    $counter = 1;
    while ($counter <=4) {
        name_person( $name_array[$i], $counter);
        $counter++;
    }
}

my output should be:

pon is person #1, zi is person #2, pol is person #3, et is person #4

Can you please help me?

  • 写回答

4条回答 默认 最新

  • douhui8454 2016-07-21 05:22
    关注

    Try this:

    <?php
    $name_array = array('pon', 'zi', 'pol', 'et');
    
    function name_person($name, $number) {
        echo $name . ' is person #' . $number . ' ';
    }
    
    for ($i = 0; $i < 4; $i++) {
        name_person( $name_array[$i], $i + 1);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?