Merge arrays in php

Asked

Viewed 3,696 times

4

Someone knows something that turns it:

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

in this?

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

3 answers

2

If you really need to name the keys the way indicated, 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 that would require a reassessment of your paradigm, which is to simply use the "array_combine".

$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
)

2

I really wanted to see another one of those wild tricks that would solve the problem without iterating but, if it’s 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 the emails has fewer elements than the names, several Notices will appear with reference to undefined indexes.

If there is no way to bring both arrays with the same length (such as a JOIN that populates the right side with Nulls), you can equalize them. A while ago I created one and it was 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 enough:

equalize( $array1, $array2 );

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

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

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

1

Assuming the variables are synchronized to each other, then they will never have different quantities, 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);

Upshot:

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

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

)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.