2

我有一个字符串中的项目列表,例如

$string=Digital SLR & Batteries (Spares if possible), Memory Cards (& Spares), Strong Walking Boots (No Trainers), Warm waterproof clothing, Packed lunch/evening meal (Or both) depending on workshop time, Drinks

我想要一个数组中的所有项目,这样我就可以使用 for 循环将它们输出到一个 html 列表中。

我怎样才能做到这一点?

4

4 回答 4

4

我看到您的数据用逗号分隔,使用explode()

$string_array = explode(",", $string);
于 2012-05-14T12:11:05.280 回答
2
$myArray = explode(',', $string);

您可以使用它来将字符串拆分为数组

于 2012-05-14T12:14:21.600 回答
2

采用:

 $pieces = explode(",", $string);

这里 $pieces 将是一个类似的数组

    $pieces[0]="Digital SLR & Batteries (Spares if possible)";

等等

于 2012-05-14T12:15:33.313 回答
2

您可以使用 exlode() 函数:

<?
$string="Digital SLR & Batteries (Spares if possible), Memory Cards (& Spares), Strong Walking Boots (No Trainers), Warm waterproof clothing, Packed lunch/evening meal (Or both) depending on workshop time, Drinks";

$arr = explode(", ", $string);
foreach ($arr as $element) {
   print $element . "\n";
}
?>
于 2012-05-14T12:16:58.183 回答