|
- //----[多维数组转换成一维数组]----
- function array_multi2single($array){
- //首先定义一个静态数组常量用来保存结果
- static $result_array = array();
- //对多维数组进行循环
- foreach ($array as $value) {
- //判断是否是数组,如果是递归调用方法
- if (is_array($value)) {
- array_multi2single($value);
- } else //如果不是,将结果放入静态数组常量
- $result_array [] = $value;
- }
- //返回结果(静态数组常量)
- return $result_array;
- }
- //----[多维数组转换成一维数组]----
复制代码 例子- $arr = array(
- array(
- 'name' => 'a',
- 'sex' => 'm',
- 'sort' => 5
- ),
- array(
- 'name' => 'c',
- 'sex' => 'm',
- 'sort' => 8
- ),
- array(
- 'name' => 'g',
- 'sex' => 'm',
- 'sort' => 3
- ),
- array(
- 'name' => 'e',
- 'sex' => 'w',
- 'sort' => 6
- ),
- array(
- 'name' => 'b',
- 'sex' => 'w',
- 'sort' => 2
- ),
- );
-
- print_r(array_multi2single($arr));
复制代码 打印结果
- Array
- (
- [0] => a
- [1] => m
- [2] => 5
- [3] => c
- [4] => m
- [5] => 8
- [6] => g
- [7] => m
- [8] => 3
- [9] => e
- [10] => w
- [11] => 6
- [12] => b
- [13] => w
- [14] => 2
- )
复制代码
|
|