1

I have a few associative arrays that I need to merge together based on there key. so:

array1:
 [person1] => tony 
 [person2] => sandra 

array2:
 [person1] => london
 [person2] => paris        

needs to be :

 array 3
  [person1] => tony , london
  [person2] => sandra , paris

The issue I'm having though is that the key could be any value , so it could be 'person1' or it could be 'hairyOtter' and the array is of varaible size.

1
  • it's not clear whether you want the values in array3 to be a string, or another array. Commented May 12, 2011 at 14:17

5 Answers 5

3

Assuming, that every is not multi-dimensional

$merged = array_merge_recursive($array1, $array2);
foreach ($merged as &$entry) {
  if (is_array($entry)) {
    $entry = implode(', ', $entry);
  }
}

The idea is, that array_merge_recursive() creates a new array, if it find two values with the same key. Everything else stays untouched.

Sign up to request clarification or add additional context in comments.

2 Comments

Better than my clunky solution :)
array_merge_recursive to the rescue!
1

I'm sure there are more efficient ways to accomplish this, but for now, this works:

<?php
function combine( $keys, $first, $second ) {
    $args = func_get_args( );
    $keys = array_shift( $args );
    $arrays = $args;
    $result = array( );

    foreach( $keys as $key ) {
        foreach( $arrays as $array ) {
            if( isset( $array[$key] ) ) {
                $result[$key][] = $array[$key];
            }
        }
    }
    return $result;
}

$first = array(
    'person1' => 'tony',
    'person2' => 'sandra'
);

$second = array(
    'person1' => 'london',
    'person2' => 'paris'
);


/**
 * To make sure you get *every* key out of both arrays.
 */
$keys = array_unique( array_merge( array_keys( $first ), array_keys( $second ) ) );

$combined = combine( $keys, $first, $second );
var_dump( $combined );

Comments

0

Two loops should do it. Iterate over each of array1 and array2, initialising array3's keys as you go to an array, and pushing into that array. Here's the first loop

$array3 = array();
foreach (array_keys($array1) as $key) {
    if(!array_key_exists($key, $array3)) {
        $array3[$key] = array();
    }
    array_push($array3[$key], $array1[$key]);
}

2 Comments

Not if there's a key in $array2 that's not in array1. With your solution, this would be ignored.
You are incorrect. My solution entirely omits $array2, but if you copy-and-pasted the entirety of the foreach, substituting array1 for array2, the key still gets in -- it's still effectively a union.
0
$array1 = array('a' => 1, 'b' => 2);
$array2 = array('a' => 3, 'b' => 4, 'c' => 5);

foreach ($array1 as $k => $v) {
  $tArray[$k][] = $v;
}
foreach ($array2 as $k => $v) {
  $tArray[$k][] = $v;
}

foreach ($tArray as $k => $v) {
  $tArray[$k] = implode(',',$tArray[$k]);
}

Comments

0

I would create a multi-dimensional array instead, unless there is a specific reason you can't use one. This way, you would have an array that looks like:

Array
(
[Person1] => Array
  (
  [Name] => Tony
  [City] => London
  )
[Person2] => Array
  (
  [Name] => Sandra
  [City] => Paris
  )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.