Merge arrays in php

4

Someone knows something that makes this:

$name = [0=>'name1',1=>'name2'];
$email = [0=>'email1',1=> 'email2'];

In this here?

$data = [ 
0 => ['name'=>'name1', 'email'=>'email1'],
1 => ['name'=>'name2', 'email'=>'email2']
];
    
asked by anonymous 02.08.2014 / 22:10

3 answers

2

I really wanted to see one of those crazy tricks that would solve the problem without iterating but, if not a problem:

foreach( $names as $offset => $name ) {
    $data[] = array( 'name' => $name, 'email' => $emails[ $offset ] );
}

There is only one small problem with this approach: If the second array with e-mails has fewer elements than names, several Notices will appear with reference to undefined indexes.

If there is no way to bring both arrays of the same length (like a JOIN that populates the right side with NULLs), you can equalize them. Some time ago I created one and it looked like this:

/**
 * Equalize array lengths
 *
 * The shorter array will receive as many NULL elements as needed
 * to have the same length of the larger array.
 *
 * Both arguments are optional, so this function can be used to
 * create dummy arrays based upon other array length
 *
 * And both arguments must be passed as reference, so the changes
 * can be applied
 *
 * @param array|optional $a
 *   First Array
 *
 * @param array|optional $b
 *   Second Array
 */
function equalize( array &$a = array(), array &$b = array() ) {

    $l1 = count( $a );
    $l2 = count( $b );

    if( $l1 == $l2 ) {
        return;
    }

    if( $l1 > $l2 ) {

        $b = array_merge( $b, array_fill( 0, ( $l1 - $l2 ), NULL ) );

    } else {

        $a = array_merge( $a, array_fill( 0, ( $l2 - $l1 ), NULL ) );
    }
}

To use just enough:

equalize( $array1, $array2 );

In case I've typed the array of names because, theoretically, names are required and optional emails, but if you want to reverse, that's fine too.

Once equalized, you can still completely ignore which arrays iterate. But obviously, in that case, the iteration would also change:

foreach( $emails as $offset => $email ) {
    $data[] = array( 'name' => $names[ $offset ], 'email' => $email );
}
    
02.08.2014 / 22:47
2

If you really need to name the keys as they are, the other answers are all good options. However, if the sizes of the two arrays are always equal and the values are aligned, there is still another much simpler possibility, but it would require a reassessment of your paradigm, which is simply to use the array_combine function.

$name = [0=>'name1',1=>'name2'];     // array com as chaves
$email = [0=>'email1',1=> 'email2']; // array com os valores

$comb = array_combine($name, $email);

The result would be:

Array
(
    [nome1]  => email1
    [nome2]  => email2
)
    
03.08.2014 / 05:37
1

Assuming that the variables are synchronized one to another, then they will never have different amounts, predicting this just do:

$name = [0=>'name1',1=>'name2'];
$email = [0=>'email1',1=> 'email2'];


foreach($name as $key => $value) {
    $data[] = array('name' => $value, 'email' => $email[$key]);
}

print_r($data);

Result:

Array
(
    [0] => Array
        (
            [name] => name1
            [email] => email1
        )

    [1] => Array
        (
            [name] => name2
            [email] => email2
        )

)
    
03.08.2014 / 00:40