How to combine multiple one-dimensional arrays into a two-dimensional array through PHP

jacklove
Release: 2023-03-30 20:58:01
Original
2367 people have browsed it

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;

}?>
Copy after login

1. Combine multiple one-dimensional arrays into two-dimensional arrays

<?php$arr1 = array(&#39;fdipzone&#39;, &#39;terry&#39;, &#39;alex&#39;);$arr2 = array(18, 19, 20);$arr3 = array(&#39;programmer&#39;, &#39;designer&#39;, &#39;tester&#39;);$keys = array(&#39;name&#39;,&#39;age&#39;,&#39;profession&#39;);$result = array_merge_more($keys, $arr1, $arr2, $arr3);

print_r($result);?>
Copy after login

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
        )

)
Copy after login
Copy after login

2. Extract multiple two-dimensional arrays Part of the data is combined into a two-dimensional array

<?php$arr1 = array(    array(&#39;name&#39;=>&#39;fdipzone&#39;),    array(&#39;name&#39;=>&#39;terry&#39;),    array(&#39;name&#39;=>&#39;alex&#39;),
);$arr2 = array(    array(&#39;age&#39;=>18),    array(&#39;age&#39;=>19),    array(&#39;age&#39;=>20),
);$arr3 = array(    array(&#39;profession&#39;=>&#39;programmer&#39;),    array(&#39;profession&#39;=>&#39;designer&#39;),    array(&#39;profession&#39;=>&#39;tester&#39;),
);$arr1 = array_column($arr1, &#39;name&#39;);$arr2 = array_column($arr2, &#39;age&#39;);$arr3 = array_column($arr3, &#39;profession&#39;);$keys = array(&#39;name&#39;,&#39;age&#39;,&#39;profession&#39;);$result = array_merge_more($keys, $arr1, $arr2, $arr3);

print_r($result);?>
Copy after login

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
        )

)
Copy after login
Copy after login

This article explains how to combine multiple one-dimensional arrays into a two-dimensional array in PHP. More For more related content, please pay attention to php Chinese website.

Related recommendations:

Explain the related methods of returning multiple columns specified in an array in PHP

About PHP based on the redis counter class Detailed explanation

Detailed explanation of php method of checking whether it matches the specified time period

The above is the detailed content of How to combine multiple one-dimensional arrays into a two-dimensional array through PHP. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!