1

I have two array in php likes this :

$a = [ 1 => [ 0 => 10, 1 => 1 ] ] and $b = [ 1 => [ 0 => 15, 1 => 3 ] ]

I have to make a union of these two arrays that resultant array should be like this :

$r = [ 1 => [ 0 => 10, 1 => 1 ], 1 => [ 0 => 15, 1 => 3 ] ]

Please give me idea how can i achieve this ..

Thanks in advance !!

2
  • 1
    This is not possible, you can't have the same index twice. Commented Jan 19, 2016 at 10:54
  • 1
    You cannot have a array with 2 keys of the same value!!!! Commented Jan 19, 2016 at 10:54

2 Answers 2

1

You can't give same array index....index is unique

$r[] = $a;
$r[] = $b;

so your array will be

$r = [ [0]=>[1 => [ 0 => 10, 1 => 1 ]], [1] =>[1 => [ 0 => 15, 1 => 3 ] ]]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot ! your solution almost fulfilled my requirement !!
0

You can't have an array with the same key.

If you want to merge values of two arrays, use array_merge:

$a = array(
           0 => 10, 
           1 => 1 ) ;
print_r($a);
echo "<br>";

$b = array(0 => 15, 
           1 => 3);

print_r($b);
echo "<br>";

$result = array_merge($a, $b);

print_r($result);

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.