0

I have two associative arrays.

array ( [apple]=>1 [banana]=>2 cocunet => 3)

and other array is

array ( [apple]=>2 [banana]=>3 cocunet => 4)

now i want to merge my array like that

array ( [apple]=>1,2 [banana]=>2,3 cocunet => 3,4)
4
  • 1
    so $array['apple'] would become an array of 1 and 2?
    – t3chguy
    Jul 21, 2014 at 13:42
  • 1
    Do you mean array ( [apple]=>array(1,2), [banana]=>array(2,3), cocunet =>array(3,4)) Jul 21, 2014 at 13:48
  • That's the same as my follow-up question just worded properly xD
    – t3chguy
    Jul 21, 2014 at 13:49
  • yes i got ans array_merge_recursive does the trick ... thanks
    – umair
    Jul 21, 2014 at 13:52

3 Answers 3

4

There is no such array in PHP. The thing you want can only be done by creating multidimentional arrays.

$a1 = array( 'apple'=>1,'banana'=>2,'coconut'=> 3);

$a2 = array( 'apple'=>2,'banana'=>3,'coconut'=> 4);

echo "<pre>";
print_r(array_merge_recursive($a1,$a2));
echo "</pre>";

For this you can use the array_merge_recursive() function.

PHPFiddle: http://3v4l.org/5OCKI

3
  • Maybe check existing answers prior to posting your own; if within a minute or so then understood but 5 minutes is a bit cheeky.
    – t3chguy
    Jul 21, 2014 at 13:50
  • The methodology of your answer is identical to the one posted five minutes prior to yours, so I do not see how this is funny whatsoever.
    – t3chguy
    Jul 21, 2014 at 13:53
  • Thanks Ozan Kurt ur solution is perfect
    – umair
    Jul 21, 2014 at 13:55
1

If you want the result to be a string then this should work:

 foreach( $array_1 as $fruit => $num) {
        if(array_key_exists($fruit, $array_2)){ //check if key from array_1 exists in array_2
             $final_array[] = array($fruit => $array_1[$fruit].','.$array_2[$fruit]); //concatenate values from shared key
        }
    }

print_r($final_array) will return:

Array
(
    [0] => Array
        (
            [apple] => 1,2
        )

    [1] => Array
        (
            [banana] => 2,3
        )

    [2] => Array
        (
            [coconut] => 3,4
        )

)
2
  • 1
    Actually it's not completely clear what he wants. He doesn't indicate whether he wants the result to be string or multidimensional array. This answer will provide string.
    – Dan
    Jul 21, 2014 at 13:52
  • But its still a nice answer mate :) Thank you.
    – Ozan Kurt
    Jul 21, 2014 at 13:52
0
<?php
    $array1 = array("apple" => 5, "banana" => 1);
    $array2 = array("banana" => 4, "coconut" => 6);
    print_r( array_merge_recursive( $array1, $array2 ) );
?>

Returns:

Array ( [apple] => 5 [banana] => Array ( [0] => 1 [1] => 4 ) [coconut] => 6 )

I only used two elements in each of the primary arrays to demonstrate the output prior to having any groups existing, i.e. non-array value.

1
  • OP @umair what return, the print_r was used to demonstrate the Output.
    – t3chguy
    Jul 21, 2014 at 13:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.