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

多维数组转一维

PHP中文网
PHP中文网Original
2016-06-01 14:33:101121browse

跳至

/**
 * 将多维数组转为一维数组
 * @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;
}


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));

           

       

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