Home >Backend Development >PHP Tutorial >PHP多维数组转字符串、多维数组转一维数组的方法

PHP多维数组转字符串、多维数组转一维数组的方法

WBOY
WBOYOriginal
2016-06-20 13:01:161159browse

PHP如何实现多维数组转字符串、多维数组转一维数组的方法,php数组与字符串的转换在开发过程中常用到,非常实用,学习PHP的朋友必看。。

PHP多维数组转字符串,默认英文逗号(,)作连接符

<?php function arrayToString($arr,$l = ",") { 
	if (is_array($arr)){ 
		return implode($l, array_map(&#039;arrayToString&#039;, $arr)); 
	} 
	return $arr; 
}
?>

PHP多维数组变成一维数组

<?php function array_multi2array($array) { 
	static $result = array(); 
	foreach($array as $key => $value) {
		if (is_array($value)){ 
			array_multi2array($value); 
		}else{
			$result[$key] = $value; 
		}
	} 
	return $result; 
}
?>

上述方法中用到了PHP的自带函数array_map()函数


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