使用 PHP 列出所有逗号分隔值

我试图列出所有逗号分隔的值。


 <?php

     $records = [

    ["WLP001","Cream Ale,Dusseldorf Altbier,Kolsch,Ordinary Bitter"],

    ["WLP002","American Amber,American Brown Ale,American India Pale Ale,American Pale Ale,American Porter,Apple Wine,Barleywine Ale,Common Perry,Cream Ale,Dusseldorf Altbier,English Brown Ale,English Cider,English India Pale Ale,English Old Ale,English Pale Ale,Extra Special Bitter,Foreign Export Stout,French Cider,Irish Dry Stout,Irish Red Ale,Kolsch,Oatmeal Stout,Ordinary Bitter,Scotch Ale (Wee Heavy),Sweet Stout,Wheat Ale"],

];


  for ($i=0; $i < count($records); $i++) { 

    $styles = explode(',',$records[$i][1]);

    echo $styles[0]."<br>";

   }

 ?>

例如


对于“WLP001” 和“WLP002”


 Cream Ale,

 Dusseldorf Altbier,

 Kolsch,

 Ordinary Bitter,

 American Amber,

 American Brown Ale,

 American India Pale Ale,

只要数据以逗号分隔,它就会继续其余部分。


因为现在的输出是

https://img1.sycdn.imooc.com/65a9d74d0001cb8603680195.jpg

拉风的咖菲猫
浏览 92回答 1
1回答

翻翻过去那场雪

由于样式可以包含多个元素,因此您需要对其进行循环并回显每个元素,而不仅仅是第一个元素。for ($i=0; $i < count($records); $i++) {&nbsp;&nbsp; &nbsp; $styles = explode(',',$records[$i][1]);&nbsp; &nbsp; for ($j=0; $j < count($styles); $j++) {&nbsp; &nbsp; &nbsp; &nbsp; echo $styles[$j]."<br>";&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP