0

I am trying to merge two arrays containing a multidimensional array and create a third array in the format below"

Here is the first multidimensional array:

Array
(
    [USA1] => Array
    (
        [0] => NewYork
        [1] => MASS
    )

[USA2] => Array
    (
        [0] => NewYork
    )

[USA3] => Array
    (
        [0] => NewYork
    )

)

This is my second multidimensional array:

Array
(
    [USA1] => Array
        (
            [NewYork] => Array
                (
                [0] => Array
                    (
                        [0] => Town1
                        [1] => Town2
                    )
            )

        [MASS] => Array
            (
                [0] => Array
                    (
                        [0] => Town3
                        [1] => Town4
                    )
            )
    )

[USA2] => Array
    (
        [NewYork] => Array
            (
                [0] => Array
                    (
                        [0] => Town1
                        [1] => Town2
                    )
            )
    )

Now i want to make 3rd array which will be merging based on there common key. If the keys are matching then i need to assign one of the values to this array in round robin fashion:

For e.g. If the value is "NewYork" under USA1 key, then i have to assign "Town1" value from another array. As this key is also present under USA2 then i have to assign "Town2" (round robin fashion). If there are more "NewYork" values are present and if have more values like "Town9" then we have to assign that value, if not present then i have to assign "Town1" value back. If the key is present only one time like "MASS" then we need to remove second value which is "Town4" in this case.

    Array
    (
        [USA1] => Array
            (
                [0] => NewYork => Town1
                [1] => Mass => Town3
            )

    [USA2] => Array
        (
            [0] => NewYork => Town2
        )

    [USA3] => Array
        (
            [0] => NewYork =>Town1
        )

)

Note: All the Array output except 3rd one is from print_r command

Greatly appreciate the help. Thank you.

1
  • Use loops and assignments to initial empty array. Commented Apr 23, 2015 at 5:47

1 Answer 1

1

I'm not exactly clear what you're asking for. It's not really a merger because in your final array [USA3] should be empty as there's no matching key in array2. And you're asking for the values to be assigned round-robin, which is not how I expect an array merge to behave.

What I'm assuming you're trying to do is evenly distribute the towns across the cities. Based on that assumption I only used values from USA1 in array two, but this will produce the third array you provided. I've updated the code example to merge the second array.

$map = array(); 
foreach ($array2 as $usa => $cities) 
{ 
    $map = $cities + $map; 
}

$towns = array();

foreach($array1 as $usa => $cityArray)
{
    while(!empty($cityArray)) 
    {
        $city = array_shift( $cityArray );
        if(empty($towns[$city]))
            $towns[$city] = $map[$city][0];

        $array3[ $usa ][] = array( $city => array_shift( $towns[$city] ) );
    }
}

This works but if you have different values in $array2['US1'] and $array2['US2'] then you should merge those two arrays before sending it to the code above. Updated code merges the second array properly.

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

6 Comments

Thank you "jfadich" for you response!! Sorry i was having type where i did not copy paste the USA3 in Array2 mentioned above. i tired using your code and it throws an error on line 2 "$towns = [];" as well as in the if loop. So i cannot able to run your code. It does not point me exact error message.
You must be running an older version of php. I updated the code to work in versions older than 5.4. Let me know if that fixes works.
Thanks again "jfadich"!! Looks like now error has gone and able to run your code but once i print print_r($array3) outside foreach loop i see all the array similar to array2 mentioned above. But if you help me to give output like Array 3 rd mentioned above that will be big help!!
Dude you are awesome!!! Your solution works perfectly in this case. As you pointed out that if US1 does not have [MASS] key but other US2 and US3 has it then it is coming as empty. Can you please help me in this case too. So it will be flexible and does not depend on any combinations
Sorry please see below. I made type above: As you pointed out that, in Array one if US1 does not have [NewYork] key, correspondingly Array2 US1 does not have [NEWYORK] but other US2 and US3 has it then it is coming as empty. Can you please help me in this case too. So it will be flexible and does not depend on any combinations
|

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.