HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Combining 3 or more arrays in php

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpmorecombiningarrays

Problem

Assuming I have 5 arrays, all just indexed arrays, and I would like to combine them, this is the best way I can figure, has anyone found a more efficient solution?

function mymap_arrays() {
    $args = func_get_args();
    $key  = array_shift($args);

    return array_combine($key, $args);
}

$keys   = array('u1', 'u2', 'u3');
$names  = array('Bob', 'Fred', 'Joe');
$emails = array('bob@mail.com', 'fred@mail.com', 'joe@mail.com');
$ids    = array(1, 2, 3);

$u_keys = array_fill( 0, count($names), array('name', 'email', 'id') );

$users  = array_combine($keys,
            array_map('mymap_arrays', $u_keys, $names, $emails, $ids)
          );


This returns:

Array
(
    [u1] => Array
        (
            [name] => Bob
            [email] => bob@mail.com
            [id] => 1
        )

    [u2] => Array
        (
            [name] => Fred
            [email] => fred@mail.com
            [id] => 2
        )

    [u3] => Array
        (
            [name] => Joe
            [email] => joe@mail.com
            [id] => 3
        )

)


EDIT: After a lot of benchmarking, this is the fastest solution I've come up with

function test_my_new() {
    $args    = func_get_args();
    $keys    = array_shift($args);
    $vkeys   = array_shift($args);
    $results = array();

    foreach($args as $key => $array) {
        $vkey = array_shift($vkeys);

        foreach($array as $akey => $val) {
            $result[ $keys[$akey] ][$vkey] = $val;
        }
    }

    return $result;
}

$keys   = array('u1', 'u2', 'u3');
$names  = array('Bob', 'Fred', 'Joe');
$emails = array('bob@mail.com', 'fred@mail.com', 'joe@mail.com');
$ids    = array(1,2,3);
$vkeys  = array('name', 'email', 'id');

test_my_new($keys, $vkeys, $names, $emails, $ids);

Solution

I would suggest the following:

function combine_keys_with_arrays($keys, $arrays) {
    $results = array();

    foreach ($arrays as $subKey => $arr)
    {
       foreach ($keys as $index => $key)
       {
           $results[$key][$subKey] = $arr[$index];    
       }
    }

    return $results;
}

$keys    = array('u1', 'u2', 'u3');
$names   = array('Bob', 'Fred', 'Joe');
$emails  = array('bob@mail.com', 'fred@mail.com', 'joe@mail.com');
$ids     = array(1,2,3);

combine_keys_with_arrays($keys, array('name'  => $names,
                                      'email' => $emails,
                                      'id'    => $ids));


The second parameter to the function can be built as you need it. It avoids array shifting and variable arguments, but I think it should be pretty fast.

Code Snippets

function combine_keys_with_arrays($keys, $arrays) {
    $results = array();

    foreach ($arrays as $subKey => $arr)
    {
       foreach ($keys as $index => $key)
       {
           $results[$key][$subKey] = $arr[$index];    
       }
    }

    return $results;
}

$keys    = array('u1', 'u2', 'u3');
$names   = array('Bob', 'Fred', 'Joe');
$emails  = array('bob@mail.com', 'fred@mail.com', 'joe@mail.com');
$ids     = array(1,2,3);

combine_keys_with_arrays($keys, array('name'  => $names,
                                      'email' => $emails,
                                      'id'    => $ids));

Context

StackExchange Code Review Q#3255, answer score: 2

Revisions (0)

No revisions yet.