PHP數組和explode函數應用實例,供大家學習參考。
例1:
<?php $province = array("北京","上海","天津","重慶","河北","山西","內蒙古","遼寧","吉林","黑龍江","江蘇","浙江","安徽","福建","江西","山東","河南","湖北","湖南","廣東","廣西","海南","四川","貴州","雲南","西藏","陝西","甘肅","寧夏","青海","新疆","香港","澳門","台灣","其他"); echo count($province);//數組成員個數 echo "<br/>"; if(is_array($province)){//檢測變量是否是數組 echo "嗯,真是個數組啊"; } echo "</br>"; for($index=0;$index<count($province);$index++){//數組循環 echo $province[$index]; echo "</br>"; } ?>
例2:
<?php //利用 explode 函數分割字符串到數組 $source = "hello1,hello2,hello3,hello4,hello5";//按逗號分離字符串 $hello = explode(',',$source); for($index=0;$index<count($hello);$index++){ echo $hello[$index];echo "</br>"; } ?>
例3:
<?php //函數挺實用的 // 分隔符可以是斜線,點,或橫線 $date = "04/30/1973"; list($month, $day, $year) = split ('[/.-]', $date); echo "Month: $month; Day: $day; Year: $year<br />\n"; ?>
以上所述就是本文的全部內容了,希望大家能夠喜歡。