PHP converts an array into a string example

Source: Internet
Author: User
Tags explode

1.implode combines array elements into strings

The connector is an example of ^:

<?php
$array = Array (' name ', ' telephone ', ' e-mail ');
$char = Implode ("^", $array);
Echo $char;
?>
Browser output:
Name ^ Telephone ^ e-mail
You can use a space as a connector:
<?php
$char = Implode ("", $array);
?>

2. Custom function

function array2string ($data, $isformdata = 1) {
if ($data = = ") return";
if ($isformdata) $data = Stripslashes ($data);
Return Addslashes (Var_export ($data, TRUE));
}

Character conversion array

Example 1. Explode () example


<?php
Example 1
$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = Explode ("", $pizza);
echo $pieces [0]; Piece1
echo $pieces [1]; Piece2
Example 2
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *
?>

Example 2. Limit parameter Example


<?php
$str = ' One|two|three|four ';
Limit of positive numbers
Print_r (Explode (' | ', $STR, 2));
Limit of negative numbers
Print_r (Explode (' | ', $STR,-1));
?>

The above example outputs:
Array
(
[0] => One
[1] => Two|three|four
)
Array
(
[0] => One
[1] => two
[2] => three
)

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.