简体   繁体   中英

Why can't I loop through an array?

I get these numbers seperated by commas from a textarea but I get an error when I try to loop through them. How do I do it? This is my code:

$numbers = $_GET['numbers'];

foreach($numbers as $number){
echo $number;
}

You should first make an array out of $numbers . You can do this by adding this line:

$numbers = explode(',', $_GET['numbers']);

Then, before you use them in the foreach loop you should use trim() to remove whitespace from the start and end:

foreach($numbers as $number){
    $number = trim($number);

    echo $number
}

如果$_GET['numbers']是用逗号分隔的列表,则它不是数组。

foreach(explode(",",$_GET['numbers']) as $number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM