将for循环的输出内容输出到数组中

时间:2018-02-20 02:20:00

标签: php arrays for-loop

{'ID': '<166.1079>', 'Date': '7 Dec 2010 16:06:42', 'Owner': 'Mary Jones'}

我有上面的循环练习。我需要将数据返回到数组中并循环并将数据打印到页面。如果你可以用我在努力学习的解释这一点的符号来分解你的答案,我将非常感激。谢谢!

2 个答案:

答案 0 :(得分:2)

$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)

答案 1 :(得分:1)

/*
 * 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>