A method of combining multiple one-dimensional combinations of PHP into two-dimensional arrays

Source: Internet
Author: User
In the development process, you need to collate the data, where more than one operation is to consolidate the various data into a set of data. This article provides a way to combine multiple one-dimensional combinations into two-dimensional arrays, providing complete code and demonstrations.

The code of the combined method is as follows, because the function variable parameter needs to be used, so PHP version 5.6 above.

<?php/** * Combine multiple one-dimensional combinations into two-dimensional arrays * * @param array  $keys define the key values of the new two-dimensional array, each corresponding to a one-dimensional array * @param  array $args Multiple one-dimensional array of arrays * @return Array */function array_merge_more ($keys, ... $arrs) {    //Check if the parameter is correct    if (! $keys | |!is_array ($keys) | |! $arrs | |!is_ Array ($arrs) | | Count ($keys)!=count ($arrs)) {        return array ();    }    The maximum length of a one-dimensional array    $max _len = 0;    Organize the data to re-index all one-dimensional arrays    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]);        }    }    Combined data    $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;}? >

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

<?PHP$ARR1 = Array (' Fdipzone ', ' Terry ', ' Alex '), $arr 2 = array (+), $arr 3 = array (' Programmer ', ' designer ', ' test Er '); $keys = Array (' name ', ' age ', ' profession '); $result = Array_merge_more ($keys, $arr 1, $arr 2, $arr 3);p Rint_r ($result) ;? >

Output:

Array (    [0] = = Array        (            [name] = Fdipzone            [age] [            profession] = programmer        ) C7/>[1] = Array        (            [name] + Terry            [age] =            [profession]        = designer)    [2] = > Array        (            [name] + Alex            [age] [            profession] = tester        ))

2. Multiple two-dimensional array extraction partial data is combined into a two-dimensional array

<?PHP$ARR1 = Array (' name ' = '    fdipzone '), Array (    ' name ' = = ' Terry '),    Array (' name ' = = ') Alex '), $arr 2 = Array (' Age ' =>18), Array (' Age '    =>19),    Array (' age ' =>20), $arr 3 = Array ( Array ('    profession ' = ' programmer '),    array (' profession ' = ' designer '),    array (' profession ' = ' tester '); $arr 1 = array_column ($arr 1, ' name '), $arr 2 = Array_column ($arr 2, ' age '); $arr 3 = Array_column ($arr 3, ' Profession '); $keys = Array (' name ', ' age ', ' profession '); $result = Array_merge_more ($keys, $arr 1, $arr 2, $arr 3);p Rint_r ( $result);? >

Output:

Array (    [0] = = Array        (            [name] = Fdipzone            [age] [            profession] = programmer        ) C6/>[1] = Array        (            [name] + Terry            [age] =            [profession] = designer        )    [2] =& Gt Array        (            [name] + Alex            [age] =            [profession] = tester        ))

This article explains the PHP multiple one-dimensional combination of two-dimensional array of methods, more relevant content please pay attention to the PHP Chinese web.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.