【问题标题】:Output contents of for loop into array将for循环的内容输出到数组中
【发布时间】:2018-02-20 02:20:00
【问题描述】:
for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       echo "the number " . $numbers1;
       echo "<br>";
  }

我上面有for循环的练习。我需要将数据返回到一个数组中并循环并将数据打印到页面上。如果您能在我尝试学习时用解释这一点的符号来分解您的答案,我将不胜感激。谢谢!

【问题讨论】:

    标签: php arrays for-loop


    【解决方案1】:
    $numbers_array = array();
    
    for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
      {
           array_push($numbers_array,$numbers1);//add the number to the array
      }
    
    print_r($numbers_array);//print out the array
    

    如果您只想打印值,可以将此行添加到 for 循环:

    var_dump($numbers_array[$numbers1-1]);//prints out the element in the position that is one less then the number you want to print out (since arrays in php are zero based)
    

    【讨论】:

    • 所以如果我没看错的话,for 语句实际上有点像一个函数,它甚至在检查语句之后运行括号中的内容?也就是说,您实际上只是用数组函数替换了我对数字的回声,将其命名为“$numbers_array,然后用 $numbers1 占据它。所以每一轮循环,数组都会添加另一个结果,直到循环停止?
    【解决方案2】:

    /*
     * Initilalize variable with an empty array.
     * Array can contain variables of any type, even other arrays and grows in size automatically.
     * In PHP 5.4 and newer you can use `$data = [];` syntax
     */
    $data = array();
    
    /*
     * Set variable $numbers1 to 1, then check that its value less than or equal to 150 and then run code between { and }
     * After the very last line in the `{ }` block perform variable increment (++$numbers1) - add 1 to the previous value.
     * Now $numbers1 becomes equal to 2, then check that its value less than or equal to 150 and again.
     * Loop will iterate over and over untill $numbers1 become eqial to 151 and `$numbers1 <= 150` evaluates to false.
     * Then loop breaks and code after closind `}` will be executed.
     */
    for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
    {
        /*
         * Set variable $value to the string. String will be built from 3 parts 
         *  - "the number ", 
         *  - string representation of $numbers1 value and
         *  - literal string "<br/>\n"
         *
         * "\n" means 'new line' symbol, in HTML is transforms into the space, but it will help you to debug your application -
         * you can see resulting code more structured in "view-source" mode of your browser (Ctrl+U in Firefox).
         * You can safely remove it.
         */
        $value = "the number " . $numbers1 . "<br/>\n";
    
        /**
         * Then add $value to the array. It is equivalent to array_push($data, $value)
         * String 'the number 1<br/>' becomes the first element (at 0 index, try to `echo $data[0];` somewhere),
         * string 'the number 2<br/>' becomes the second one ($data[1]) and so on.
         */
        $data[] = $value;
    }
    
    /* Tt is good habit to unset variables that you don not need anymore.
     * Try to `echo $value` and you will got Notice - variable does not exist.
     */
    unset ($value, $numbers1);
    
    /*
     * For page output you should use some kind of templating engine (Twig, for example). For now, we will use PHP itself
     */
    
    // After this line starts plain HTML, PHP engine will output is as is. Like web server usially does.
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <title>Demo!</title>
        </head>
        <body>
            <p>
                <?php 
                    /*
                     * Meet PHP code again.
                     * All that printed or echoed will be put in place of the code between open and closing php tags
                     */
                    foreach ($data as $value) {
                        echo $value;
                    }
                ?>
            </p>
        </body>
    </html>
    

    【讨论】:

    • 哇,你必须以教书为生。我将把它保存为参考资料。 1 个问题,如果我在 echo 语句之前插入一个 Print_r,它会同时显示 print_r 和 echo,还是 print_r 在输出时清除数组?
    • 你会怎么称呼它?你没有像我预期的那样使用“array_push”。
    • @DanMan3395, print_r echo $array; 会产生解析错误。 echoprintprint_r 不修改它们的参数。 $array[] = 'val acts 与 array_push 有点不同,但对于此代码示例,它们是等价的。
    猜你喜欢
    • 2020-02-27
    • 2021-09-12
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多