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

WBOY
Release: 2016-06-20 13:01:16
Original
1126 people have browsed it

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

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

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

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; 
}
?>
Copy after login

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


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template