php 分解为数组并将每个元素打印为列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16257063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Explode to array and print each element as list item
提问by user2170133
I have a set of numbers in a table field in database, the numbers are separated by comma ','. I am trying to do the following:
我在数据库的表字段中有一组数字,这些数字用逗号“,”分隔。我正在尝试执行以下操作:
Step 1. : SELECT set of numbers from database and explode it to array :
第 1 步:从数据库中选择一组数字并将其分解为数组:
$array = explode(',', $set_of_numbers);
Step 2. : Print each element of the array as list item by using foreach loop
:
第 2 步:使用foreach loop
以下命令将数组的每个元素打印为列表项:
foreach ($array as $list_item => $set_of_numbers){
echo "<li>";
print_r(array_list_items($set_of_numbers));
echo "</li>";}
Please anybody tell me what is wrong. Thank you.
请任何人告诉我出了什么问题。谢谢你。
回答by Danil Speransky
$numbers = '1,2,3';
$array = explode(',', $numbers);
foreach ($array as $item) {
echo "<li>$item</li>";
}
回答by Marc B
Assuming your original $set_of_numbers
is simply a CSV string, something like 1,2,3,4,...
, then your foreach is "mostly" ok. But your variable naming is quite bonkers, and your print-r() call uncesary:
假设您的原始文件$set_of_numbers
只是一个 CSV 字符串,例如1,2,3,4,...
,那么您的 foreach 就“基本上”没问题。但是你的变量命名非常疯狂,你的 print-r() 调用 uncesary:
$array = explode(',', $set_of_numbers);
foreach($array as $key => $value) {
echo "<li>$key: $value</li>";
}
Assuming that 1,2,3,4... string, you'd get
假设 1,2,3,4... 字符串,你会得到
<li>0: 1</li>
<li>1: 2</li>
<li>2: 3</li>
etc...
回答by antelove
$numbers = "1,2,3";
$array = explode(",", $numbers);
/* count length of array */
$arrlength = count($array);
/* using for while */
$x = 0;
while ($x < $arrlength) {
echo "<li>$array[$x]</li>" . PHP_EOL;
$x++;
}
echo PHP_EOL;
/* using for classic */
for ($x = 0; $x < $arrlength; $x++) {
echo "<li>$array[$x]</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc */
foreach ($array as $value) {
echo "<li>$value</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc key */
foreach ($array as $key => $value) {
echo "<li>$key => $value</li>" . PHP_EOL;
}
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/ZqT4Yi" ></iframe>
回答by user49828
Here is answer for your question to get ride of your problem
这是您的问题的答案,以解决您的问题
$Num = '1,2,3,4,5,';
$Array = explode(',',$Num);
foreach ($Array as $Items)
{
echo "<li>&Items</li>"; // This line put put put in the list.
}
回答by Pranav Rana
This can easily be achieved by the following code snippet:
这可以通过以下代码片段轻松实现:
<?php
$my_numbers = '1,12,3.2,853.3,4545,221';
echo '<ul>';
foreach(explode(',', $my_numbers) AS $my_number){
echo '<li>'.$my_number.'</li>';
}
echo '</ul>';
The above code will output the following HTML:
上面的代码将输出以下 HTML:
<ul><li>1</li><li>12</li><li>3.2</li><li>853.3</li><li>4545</li><li>221</li></ul>
Credits: http://dwellupper.io/post/49/understanding-php-explode-function-with-examples
学分:http: //dwellupper.io/post/49/understanding-php-explode-function-with-examples