Home >Backend Development >PHP Tutorial >How to combine multiple one-dimensional numbers into a two-dimensional array in PHP
During the development process, data needs to be organized, and one of the most common operations is to integrate various data into a set of data. This article provides a method for combining multiple one-dimensional arrays into a two-dimensional array, and provides complete code and demonstrations.
The code of the combination method is as follows. Since function variable parameters need to be used, PHP version 5.6 or above is required.
<?php/** * 将多个一维数组合拼成二维数组 * * @param Array $keys 定义新二维数组的键值,每个对应一个一维数组 * @param Array $args 多个一维数组集合 * @return Array */function array_merge_more($keys, ...$arrs){ // 检查参数是否正确 if(!$keys || !is_array($keys) || !$arrs || !is_array($arrs) || count($keys)!=count($arrs)){ return array(); } // 一维数组中最大长度 $max_len = 0; // 整理数据,把所有一维数组转重新索引 for($i=0,$len=count($arrs); $i<$len; $i++){ $arrs[$i] = array_values($arrs[$i]); if(count($arrs[$i])>$max_len){ $max_len = count($arrs[$i]); } } // 合拼数据 $result = array(); for($i=0; $i<$max_len; $i++){ $tmp = array(); foreach($keys as $k=>$v){ if(isset($arrs[$k][$i])){ $tmp[$v] = $arrs[$k][$i]; } } $result[] = $tmp; } return $result; }?>
<?php$arr1 = array('fdipzone', 'terry', 'alex');$arr2 = array(18, 19, 20);$arr3 = array('programmer', 'designer', 'tester');$keys = array('name','age','profession');$result = array_merge_more($keys, $arr1, $arr2, $arr3); print_r($result);?>
Output:
Array( [0] => Array ( [name] => fdipzone [age] => 18 [profession] => programmer ) [1] => Array ( [name] => terry [age] => 19 [profession] => designer ) [2] => Array ( [name] => alex [age] => 20 [profession] => tester ) )
<?php$arr1 = array( array('name'=>'fdipzone'), array('name'=>'terry'), array('name'=>'alex'), );$arr2 = array( array('age'=>18), array('age'=>19), array('age'=>20), );$arr3 = array( array('profession'=>'programmer'), array('profession'=>'designer'), array('profession'=>'tester'), );$arr1 = array_column($arr1, 'name');$arr2 = array_column($arr2, 'age');$arr3 = array_column($arr3, 'profession');$keys = array('name','age','profession');$result = array_merge_more($keys, $arr1, $arr2, $arr3); print_r($result);?>
Output:
Array( [0] => Array ( [name] => fdipzone [age] => 18 [profession] => programmer ) [1] => Array ( [name] => terry [age] => 19 [profession] => designer ) [2] => Array ( [name] => alex [age] => 20 [profession] => tester ) )
This article explains how to combine multiple one-dimensional arrays into a two-dimensional array in PHP, more For related content, please pay attention to php Chinese website.
Related recommendations:
php method to return multiple columns specified in an array
mysql secure-file-priv option issue The solution
php uses the debug_backtrace method to track code calls
The above is the detailed content of How to combine multiple one-dimensional numbers into a two-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!