Home >Backend Development >PHP Tutorial >Analysis of path method for converting php array to string to obtain array representation

Analysis of path method for converting php array to string to obtain array representation

黄舟
黄舟Original
2017-09-02 09:39:221718browse

This article mainly introduces the path for PHP to obtain array representation, and compares and analyzes the implementation techniques of converting arrays to strings in the form of examples. Friends in need can refer to the following

The examples in this article describe how PHP obtains array representation. path method. Share it with everyone for your reference. The details are as follows:

Problem:

During the file parsing process, it was found that a path is stored in the form of an array. Now it is necessary to store the complete path in characters String format output

Solution:


$hostspath=array('Windows','System32','drivers','etc','hosts');
$pathstr='';
foreach($hostspath as $k=>$v){
 $pathstr.=$v.'/';
}
$pathstr=substr($pathstr,0,-1);
echo $pathstr;

Output:


Windows/System32/drivers/etc/hosts

After writing the above code, I thought that this is a problem of converting an array to a string, and a simpler method can be used!

Improvement method:


##

$hostspath=array('Windows','System32','drivers','etc','hosts');
$pathstr=implode('/',$hostspath);
echo $pathstr;

The output result is also:


Windows/System32/drivers/etc/hosts

Summary:

Using PHP’s own system functions to solve problems is often simpler and more efficient than the algorithms you come up with yourself!

The above is the detailed content of Analysis of path method for converting php array to string to obtain array representation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn