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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-20 13:01:16
Original
1173 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
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template