Analysis of path method for array representation of PHP array to string

Source: Internet
Author: User
Tags array to string
This article mainly introduces PHP to get array representation of the path, combined with instance form to compare the implementation of the array to the string, the need for friends can refer to the following

In this paper, we describe the path method of getting array representation in PHP. Share to everyone for your reference, as follows:

Problem:

A path is found to be stored as an array in the file parsing process, and the full path is now required to be output as a string

Workaround:


$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, think of this as an array to string problem, you can also use a more simple Method!

Improved method:


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

The output results are also:


Windows/system32/drivers/etc/hosts

Summarize:

Using PHP's own system functions to solve the problem is often more simple and efficient than the algorithm you come up with!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.