Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array combine three or more arrays with php

Tags:

arrays

php

ok, assuming I have 5 arrays, all just indexed arrays, and I would like to combine them, this is the best way I can figure, is there a better way to handle this?

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('[email protected]','[email protected]','[email protected]');
$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] => [email protected]
            [id] => 1
        )

    [u2] => Array
        (
            [name] => Fred
            [email] => [email protected]
            [id] => 2
        )

    [u3] => Array
        (
            [name] => Joe
            [email] => [email protected]
            [id] => 3
        )

)

EDIT: After lots of benchmarking I wend with a version of Glass Robots answer to handle a variable number of arrays, it's slower than his obviously, but faster than my original:

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('[email protected]','[email protected]','[email protected]');
$ids=array(1,2,3);
$vkeys=array('name','email','id');
test_my_new($keys,$vkeys,$names,$emails,$ids);
like image 784
Trey Avatar asked Jul 01 '11 22:07

Trey


People also ask

Which function combines or merges two or more arrays?

array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array.

Which PHP function is used to combine arrays?

The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like.

What is the difference between array_merge () and Array_merge_recursive () in PHP?

The array_merge_recursive() function merges one or more arrays into one array. The difference between this function and the array_merge() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.


3 Answers

Personally for readability I would do it this way:

$keys   = array('u1','u2','u3');
$names  = array('Bob','Fred','Joe');
$emails = array('[email protected]','[email protected]','[email protected]');
$ids    = array(1,2,3);
$result = array();

foreach ($keys as $id => $key) {
    $result[$key] = array(
        'name'  => $names[$id],
        'email' => $emails[$id],
        'id'    => $ids[$id],
    );
}
like image 128
Glass Robot Avatar answered Oct 23 '22 12:10

Glass Robot


Here's basically a one-liner for a set number of elements:

$combined = array_combine($keys, array_map(function ($id, $name, $email) {
                return compact('id', 'name', 'email');
            }, $ids, $names, $emails));

And here a version for PHP 5.2- without anonymous functions:

$combined = array_combine($keys, array_map(create_function('$id, $name, $email',
                'return compact("id", "name", "email");'
            ), $ids, $names, $emails));

For a variable number of elements, it'll look like this:

function combineValues($keys, $values) {
    $vKeys = array_keys($values);
    return array_combine($keys, array_map(
               function ($values) use ($vKeys) { return array_combine($vKeys, $values); },
               call_user_func_array('array_map', array_merge(
                   array(function () { return func_get_args(); }),
                   $values))));
}

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

I have to admit that looks pretty cryptic, so here's an expanded version:

function combineValues($keys, $values) {
    $valueKeys = array_keys($values);
    $combinedValues = call_user_func_array('array_map', array_merge(array(function () { return func_get_args(); }), $values));
    $combinedValues = array_map(function ($values) use ($valueKeys) { return array_combine($valueKeys, $values); }, $combinedValues);
    return array_combine($keys, $combinedValues);
}

For PHP 5.2- this may look like this:

function combineValues($keys, $values) {
    $result = call_user_func_array('array_map', array_merge(
                  array(create_function('', 'return func_get_args();')),
                  $values));

    array_walk($result,
               create_function('&$val, $i, $keys', '$val = array_combine($keys, $val);'),
               array_keys($values));

    return array_combine($keys, $result);
}
like image 32
deceze Avatar answered Oct 23 '22 12:10

deceze


I did not check the efficiency, but in my solution I am using only standard functions (they should be optimized) without custom mapping and looping outside them:

// assigning the data from your question
$keys = array('u1','u2','u3');
$names = array('Bob','Fred','Joe');
$emails = array('[email protected]','[email protected]','[email protected]');
$ids = array(1,2,3);

// creating resulting array
$result = array_merge_recursive(
    array_combine($keys, $names),
    array_combine($keys, $emails),
    array_combine($keys, $ids)
);

Did you try this solution?

like image 7
Tadeck Avatar answered Oct 23 '22 13:10

Tadeck