今日紹介するものPHP配列を文字列に変換したimplode()
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$vegetables[0] = "corn"; </SPAN><LI class=alt><SPAN>$vegetables[1] = "broccoli"; </SPAN><LI class=""><SPAN>$vegetables[2] = "zucchini"; </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>text</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>implode</FONT></SPAN><SPAN>(",", $vegetables); </SPAN></SPAN><LI class=""><SPAN>echo $text; </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span><span></span> </li></ol>
実行結果
コーン、ブロッコリー、ズッキーニ
2 PHP文字列を配列に変換したexplode()
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>text</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"corn, broccoli, zucchini"</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>vegetables</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>explode</FONT></SPAN><SPAN>(", ", $text); </SPAN></SPAN><LI class=""><SPAN>print_r($vegetables); </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
実行結果:
<ol class="dp-xml"> <li class="alt"><span><span>Array </span></span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> corn </span> </li> <li class=""> <span>[1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> broccoli </span> </li> <li class="alt"> <span>[2] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> zucchini </span> </li> <li class=""><span>) </span></li> </ol>
上記の 2 つのコードは、PHP 配列から文字列への変換と、PHP 文字列から配列への変換について紹介する関連コードです。