如何将Split/Explode一个字符串转换成PHP中的编号列表?
How to Split/Explode a string into a numbered lists in PHP?
我正在尝试将此字符串拆分为编号列表..
str 选项是“你好,你好,你好”。
我需要输出为
- 你好
- 你好
- 你好
这是我的代码
$newstr = 'My values are' . explode(",", $str["options"]) . '<br/>';
但是,这会导致只打印单词 Array。请帮我解决这个问题:((
a) explode()
用逗号将字符串作为数组。
b) 遍历它并连接数字和文本。
c) 将所有这些连接的值保存到一个字符串中。
d) echo
这个字符串在最后。
<?php
$str["options"] = "Hello,Howdy,Hola";
$data = explode(",", $str["options"]);
$newstr = 'My values are'. PHP_EOL;
foreach($data as $key => $value){
$number = $key+1;
$newstr .= "$number.". $value . PHP_EOL;
}
echo $newstr;
注意:- 您也可以使用 <br>
而不是 PHP_EOL
。
试试这个,它会在 html 列表标签中打印出来
<ol>
<?php
$option = '';
$newstr = explode(",", $str["options"]) ;
foreach($newstr as $option) {
$option = trim($option); ?>
<li><?php echo $option; ?></li>
<?php } ?>
</ol>
首先,您需要explode your array
. That will create an indexed array, which starts the index from 0 up to 2. Next, you will need to implode it in some manner. Other answers are more explicit and use foreach
, so this one provides an alternative, where you convert your array into the desired format via array_map. The function
passed to array_map
is a so-called callback, a function
that is executed by array_map
for each element, in this case, which is aware of the key and the value of each element. Now, we implode our input and, to make sure that we have some keys, we pass the result of array_keys。
$input = explode(",", "Hello,Howdy,Hola");
$output = implode('<br>', array_map(
function ($v, $k) {
return ($k + 1).'. '.$v;
},
$input,
array_keys($input)
));
我正在尝试将此字符串拆分为编号列表..
str 选项是“你好,你好,你好”。
我需要输出为
- 你好
- 你好
- 你好
这是我的代码
$newstr = 'My values are' . explode(",", $str["options"]) . '<br/>';
但是,这会导致只打印单词 Array。请帮我解决这个问题:((
a) explode()
用逗号将字符串作为数组。
b) 遍历它并连接数字和文本。
c) 将所有这些连接的值保存到一个字符串中。
d) echo
这个字符串在最后。
<?php
$str["options"] = "Hello,Howdy,Hola";
$data = explode(",", $str["options"]);
$newstr = 'My values are'. PHP_EOL;
foreach($data as $key => $value){
$number = $key+1;
$newstr .= "$number.". $value . PHP_EOL;
}
echo $newstr;
注意:- 您也可以使用 <br>
而不是 PHP_EOL
。
试试这个,它会在 html 列表标签中打印出来
<ol>
<?php
$option = '';
$newstr = explode(",", $str["options"]) ;
foreach($newstr as $option) {
$option = trim($option); ?>
<li><?php echo $option; ?></li>
<?php } ?>
</ol>
首先,您需要explode your array
. That will create an indexed array, which starts the index from 0 up to 2. Next, you will need to implode it in some manner. Other answers are more explicit and use foreach
, so this one provides an alternative, where you convert your array into the desired format via array_map. The function
passed to array_map
is a so-called callback, a function
that is executed by array_map
for each element, in this case, which is aware of the key and the value of each element. Now, we implode our input and, to make sure that we have some keys, we pass the result of array_keys。
$input = explode(",", "Hello,Howdy,Hola");
$output = implode('<br>', array_map(
function ($v, $k) {
return ($k + 1).'. '.$v;
},
$input,
array_keys($input)
));