1

I have 2 arrays

$array1 = array(
    ['2013-05-01']=>'test',
    ['2013-05-02']=>'testing',
    ['2013-05-03']=>'working',
    ['2013-05-04']=>'future test');

$array2 = array(
    ['2013-05-01']=>'1',
    ['2013-05-02']=>'done',
    ['2013-05-03']=>'code',
    ['2013-05-05']=>'release');

I want to join these array, so that output is

$result = array(
        ['2013-05-01']=>'test 1', 
        ['2013-05-02']=>'testing 2',
        ['2013-05-03']=>'working code',
        ['2013-05-04']=>'future test',
        ['2013-05-05']=>'release')

I tried $result = $array1 + array2; array_merge() , array_combine() none gave the correct result.

Can you help me please.

6
  • 3
    You need to write your own code to do this. There is no built in functions. Commented May 22, 2013 at 2:09
  • you mean using foreach loop? Commented May 22, 2013 at 2:14
  • 3
    It keeps surprising me that people expect PHP to have a builtin function for every single scenario that a starting programmer should be able to solve without effort. Commented May 22, 2013 at 2:14
  • function array_join($arr1, $arr2) {return array(['2013-05-01']=>'test 1', ['2013-05-02']=>'testing 2',['2013-05-03']=>'working code');} - just messing ;) Commented May 22, 2013 at 2:14
  • Your given code doesn't even compile properly. Commented May 22, 2013 at 2:20

5 Answers 5

6
foreach($array2 AS $key => $value)
{
    $array1[$key] = isset($array1[$key]) ? $array1[$key] . " " . $value : $value;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Error handling for what? The case that $array1 doesn't contain the keys of $array2 is taken care of.
I suggest to use isset() instead of empty().
Good suggestion. If $array1 would contain a '0' as value, it would be deleted this way.
@zerkms There are a lot of empty() calls? :))) empty($array1[$key]) suggested to be isset($array1[$key]).
This has the potentially unintended side effect of modifying $array1.
|
6

Here is a one-line solution for the issue:

$array1 = array(
    '2013-05-01'=>'test',
    '2013-05-02'=>'testing',
    '2013-05-03'=>'working',
    '2013-05-04'=>'future test');

$array2 = array(
    '2013-05-01'=>'1',
    '2013-05-02'=>'done',
    '2013-05-03'=>'code',
    '2013-05-05'=>'release');


$r = array_map(function($i) {
    return !is_array($i) ? $i : implode(' ', $i);
}, array_merge_recursive($array1, $array2));

var_dump($r);

Online demo: http://ideone.com/wmbple

4 Comments

Dammit I spent 5 minutes typing this out on crappy iPad softkeys just to have some slacker with a real keyboard beat me to the punch.
@Niels Keurentjes: "just to have some slacker with a real keyboard beat me to the punch" --- "slacker"? Is it a joke?
@NielsKeurentjes For someone typing answers on an iPad, it's debatable who's the slacker =p
@Jack have you ever tried? All the curlies and everything are hidden behind like 5 keypresses each, and you have to deal with Apple's idea of capitalization :P
1

You could use array_walk():

$result = $array1;
array_walk($result, function(&$value, $key) use (&$array2) {
    $value .= ' ' . $array2[$key];
});

Demo

This modifies $result (copied from $array1) in-place with values looked from $array2 based on the array keys.

Comments

0

It's better to write a function for it and keep it in your toolkit:

function merge_array() {
    $merged = array();
    $arrays = func_get_args();

    if ($arrays) {
        foreach ($arrays as $array) {
            foreach ($array as $key => $value) {
                if (isset($merged[$key])) {
                    $merged[$key] .= ' ' . $value;
                }
                else {
                    $merged[$key] = $value;
                }
            }
        }
    }

    return $merged;
}

Now pass $array1 and $array2 to it:

$result = merge_array($array1, $array2);

And you will get this result:

// Array
// (
//     [2013-05-01] => test 1
//     [2013-05-02] => testing done
//     [2013-05-03] => working code
//     [2013-05-04] => future test
//     [2013-05-05] => release
// )

Note that this function accept more than two arrays if you want.

  • [!] Check it online at ideone.

Comments

0

array_walk() would be the easiest way to do it.

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.