0

I've following two arrays as follows,

$array1 = array
    (
        'Name' => 'Scott',
        'Department' =>  'Sales',
        'Location' => 'USA',
        'Details' => array
            (
                15 => '90%', 
                16 => '75%',
                17 => '50%',
                18 => '60%',
                19 => '50%',
                20 => '45%',  
            ),
          );
$array2 = array
(
    'Action' => 'Imp',
    'Name' => 'Scott',
    'Department' => 'Sales',
    'Location' => 'Canada', 
    'Details' => array 
    (
             15 => '20%', 
             16 => '15%',
             17 => '25%',
             18 => '10%',
    ),
);

Result should be:

$result_arr = array
        (
            'Action' => 'Imp',
            'Name' => 'Scott',
            'Department' =>  'Sales',
            'Location' => 'Canada',
            'Details' => array
                (
                    15 => '20%', 
                    16 => '15%',
                    17 => '25%',
                    18 => '10%',
                    19 => '50%',
                    20 => '45%',  
                ),
              );

I've tried array_merge and array union (+) operator but none of them is fit for my requirement.

7
  • 1
    Can you provide more detail about the merge requirement? What causes the value of 'sales' to be retained over 'Sales'? 'Canada' over 'USA'? Commented May 30, 2013 at 14:37
  • @GeorgeCummins: its typo mistake,now I've corrected the question Commented May 30, 2013 at 14:41
  • You answerd the sales/Sales part, but not the USA/Canada part. Commented May 30, 2013 at 14:43
  • is the custom_field in the $array2 a typo as well or its a business logic that requires it to be merged into Details key? Commented May 30, 2013 at 14:43
  • What are you getting when you merge them? Can you show us the code you use to merge? Commented May 30, 2013 at 14:45

5 Answers 5

1

Try this

$final= array_merge($array1,$array2);
$final['Details']= $final['Details']+$array1['Details'];
asort($final);
echo "<pre>"; print_r($final);

Output :

Array
(
    [Location] => Canada
    [Action] => Imp
    [Department] => Sales
    [Name] => Scott
    [Details] => Array
        (
            [15] => 20%
            [16] => 15%
            [17] => 25%
            [18] => 10%
            [19] => 50%
            [20] => 45%
        )

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

Comments

1

It seems that resulting array should look like $array2 and $array2['Details'] should be extended with keys from $array1['Details']

$result_arr = $array2;
$result_arr['Details'] += $array1['Details'];

Comments

1
array_replace_recursive($array1, $array2);

Comments

0

With as little info that was provided, I was able to get your result using this:

$array = array_merge($array1, $array2);
$array['Details'] = $array2['Details'] + $array1['Details'];
print_r($array);

The Result:

Array
(
    [Name] => Scott
    [Department] => Sales
    [Location] => Canada
    [Details] => Array
        (
            [15] => 20%
            [16] => 15%
            [17] => 25%
            [18] => 10%
            [19] => 50%
            [20] => 45%
        )

    [Action] => Imp
)

3 Comments

There is no key 'custom_fields',its my typo mistake.Please check updated question,thanks.
When I use array_merge, I get your output, I don't know why you say it doesn't work?
if we use array_merge then Details array is vary with my requirement.Thanks
0
$array2['Details'] = array_merge($array1['Details'], $array2['Details']);
$result = $array2;

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.