我们在实际使用PHP进行字符串和数组之间进行转换时,implode和explode两个函数将会比较有用。implode() 函数进行PHP数组转字符串,explode() 函数则是用来实现PHP字符串转数组的。

一、PHP数组转字符串 implode()

1<?php
2$vegetables[0] = "corn";
3$vegetables[1] = "broccoli";
4$vegetables[2] = "zucchini";
5$text = implode(",", $vegetables);
6echo $text;
7?>

运行结果为:corn,broccoli,zucchini

二、PHP字符串转数组 explode()

 1<?php
 2$text = "corn, broccoli, zucchini";
 3$vegetables = explode(", ", $text);
 4print_r($vegetables);
 5?>
 6其运行结果为:
 7Array
 8(
 9[0] => corn
10[1] => broccoli
11[2] => zucchini
12)