1

I have 2 separate multidimensional arrays that have the same structure. Example:

Array
(
    [0] => Array
        (
            [uploadData] => 1234 Main St
        )

    [1] => Array
        (
            [uploadData] => 5678 Elm St
        )

    [2] => Array
        (
            [uploadData] => 9879 New St
        )

    [3] => Array
        (
            [uploadData] => 9876 Shady Lane
        )

)

Array
(
    [0] => Array
        (
            [uploadData] => Orlando
        )

    [1] => Array
        (
            [uploadData] => Tampa
        )

    [2] => Array
        (
            [uploadData] => Miami
        )

    [3] => Array
        (
            [uploadData] => West Palm Beach
        )

)

I just need to get them into 1 new array that looks like this:

Array
(
    [0] => Array
        (
            [uploadData] => 1234 Main St Orlando
        )

    [1] => Array
        (
            [uploadData] => 5678 Elm St Tampa
        )

    [2] => Array
        (
            [uploadData] => 9879 New St Miami
        )

    [3] => Array
        (
            [uploadData] => 9876 Shady Lane West Palm Beach
        )

)

I've been trying to use array_merge but I cannot get it to work.

4
  • You're going to have to build a custom function to do this. Have you tried that yet? Commented Dec 20, 2012 at 23:06
  • 1
    No, I was not 100% sure what built in functions I should even be using. Trying to figure that out now. Commented Dec 20, 2012 at 23:08
  • what if the arrays have different length ? Commented Dec 20, 2012 at 23:09
  • They will be the exact same length. I am sure of that. Commented Dec 20, 2012 at 23:18

4 Answers 4

3

Simple, recursive, key-independent solution:

function array_concat_recursive($array1, $array2){
    $result = array();
    foreach($array1 as $key => $value){
        if(isset($array2[$key])){
            if(is_array($value)){
                $result[$key] = array_concat_recursive($value, $array2[$key]);
            }else{
                $result[$key] = $value . ' ' . $array2[$key];
            }
        }
    }
    return $result;
}
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that:

  1. Your first array is assigned to a variable called $address
  2. Your second array is assigned to a variable called $city
  3. There are ALWAYS exact matches between the two arrays

EDIT:
Great catch by @dev-null-dweller - edited to catch full depth of arrays.

NOTE
Example code in question does not have quotes around uploadData key, so that is replicated here:

The following will do what you want:

foreach($address as $key=>$value) {
    $newarray = array(
         'uploadData'=>$value['uploadData'] . ' ' . $city[$key]['uploadData'];
    );
}

The $newarray will contain an array structured per your request.

4 Comments

Nope, you are missing one level of arrays. As it is now it will trigger warnings and result in array full of entries like Array Array
Thanks cale_b. Your assumptions are correct but dev-null-dweller is right I got a bunch of - [0] => Array Array [1] => Array Array [2] => Array Array [3] => Array Array
Huh? You're just re-assigning $newarray over and over again... You want to do $newarray[] = and you need to initialize it before the loop.
Yeah I ended up with an array with just the values from the last row. Thanks for the help though
1

Fetch from both arrays until everything is gone:

$final = [];
$key   = 'uploadData';
while ($array1 && $array2)
{
    $final[][$key] = array_shift($array1)[$key] . ' ' . array_shift($array2)[$key];
}

See array_shift and converting to booleans.

4 Comments

Nice way to alter array_merge with specific items order, but this not answer the questions, there is no concatenations of values here.
@dev-null-dweller: What do you mean? $final should be correct.
no, for sample data posted by OP your final array will have 8 items, when only 4 are desired.
Ah, now seeing, well then my answer is totally wrong even. However, it can be quickly adopted. Edit: Done. Edit2: Demo: eval.in/5018
0

Because the two arrays have the same size and there are no associative first level keys to preserve, they can be mapped together with array_map() and concatenated.

Code: (Demo)

$streets = [
    ['uploadData' => '1234 Main St'],
    ['uploadData' => '5678 Elm St'],
    ['uploadData' => '9879 New St'],
    ['uploadData' => '9876 Shady Lane'],
];

$cities = [
    ['uploadData' => 'Orlando'],
    ['uploadData' => 'Tampa'],
    ['uploadData' => 'Miami'],
    ['uploadData' => 'West Palm Beach'],
];

var_export(
    array_map(
        fn($s, $c) => ['uploadData' => "{$s['uploadData']} {$c['uploadData']}"],
        $streets,
        $cities
    )
);

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.