Home > php教程 > PHP源码 > 多维数组转一维

多维数组转一维

PHP中文网
Release: 2016-06-01 14:33:10
Original
1121 people have browsed it

跳至

/**
 * 将多维数组转为一维数组
 * @author echo* @param array $arr
 * @return array
 */
function ArrMd2Ud($arr) {
	#将数值第一元素作为容器,作地址赋值。
	$ar_room = &$arr[key($arr)];
	#第一容器不是数组进去转呀
	if (!is_array($ar_room)) {
		#转为成数组
		$ar_room = array($ar_room);
	}
	#指针下移
	next($arr);
	#遍历
	while (list($k, $v) = each($arr)) {
		#是数组就递归深挖,不是就转成数组
		$v = is_array($v) ? call_user_func(__FUNCTION__, $v) : array($v);
		#递归合并
		$ar_room = array_merge_recursive($ar_room, $v);
		#释放当前下标的数组元素
		unset($arr[$k]);
	}
	return $ar_room;
}
Copy after login


2. [代码]

$arr = array(1, 2, 3 => array(1, 2, 'ar' => array(1, 2 => array('a', 'b'))), array('ar' => array(3, 4)));
print_r(ArrMd2Ud($arr));
Copy after login

           

       

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template