本文實例講述了PHP獲取數組表示的路徑方法。分享給大家供大家參考,具體如下:
問題:
文件解析過程中發現一段路徑用數組的形式存儲,現需要將完整路徑以字符串形式輸出
解決方法:
$hostspath=array('Windows','System32','drivers','etc','hosts');$pathstr='';foreach($hostspath as $k=>$v){ $pathstr.=$v.'/';}$pathstr=substr($pathstr,0,-1);echo $pathstr;
輸出:
Windows/System32/drivers/etc/hosts
寫完上述代碼后想到這是一個數組轉字符串的問題,還可以使用更簡單的方法!
改進方法:
$hostspath=array('Windows','System32','drivers','etc','hosts');$pathstr=implode('/',$hostspath);echo $pathstr;
輸出結果同樣為:
Windows/System32/drivers/etc/hosts
總結:
用PHP自帶的系統函數解決問題往往比自己想出的算法來得簡單、高效!
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選