5

I Want to Union 2 array $A and $B Example:

$A = Array(
    0=>array(
            'lable' =>"label0",
            'id_poste'=>1,
            'id_part'=>11
    ),
    1=>array(
            'lable' =>"label1",
            'id_poste'=>2,
            'id_part'=>12
            ),
    2=>array(
            'lable' =>"label2",
            'id_poste'=>3,
            'id_part'=>13
    ),
    3=>array(
            'lable' =>"label3",
            'id_poste'=>4,
            'id_part'=>14
            )
);

$B = Array(
    0=>array(
            'lable' =>"label0",
            'id_poste'=>1,
            'id_part'=>11
    ),
    1=>array(
            'lable' =>"label1_X",
            'id_poste'=>2,
            'id_part'=>12
            ),
    2=>array(
            'lable' =>"label2",
            'id_poste'=>3,
            'id_part'=>13
    ),
    3=>array(
            'lable' =>"label3_X",
            'id_poste'=>4,
            'id_part'=>14
            )
);

The result of union between these two array will be

/*
$result => Array(
    0=>array(
            'lable' =>"label0",
            'id_poste'=>1,
            'id_part'=>11
    ),
    1=>array(
            'lable' =>"label1",
            'id_poste'=>2,
            'id_part'=>12
            ),
    2=>array(
            'lable' =>"label1_X",
            'id_poste'=>2,
            'id_part'=>12
            )       
    3=>array(
            'lable' =>"label2",
            'id_poste'=>3,
            'id_part'=>13
    ),
    4=>array(
            'lable' =>"label3",
            'id_poste'=>4,
            'id_part'=>14
            ),
    5=>array(
            'lable' =>"label3_X",
            'id_poste'=>4,
            'id_part'=>14
            )
);
*/

I have tried with :

$C = $A+$B;
echo '<pre>';
print_r($C);
echo '</pre>'

But the result is not what I expected ? Anybody could help please?

Thanks

EIDT:

if using array_merge($A, $B);

Array
(
[0] => Array
    (
        [lable] => label0
        [id_poste] => 1
        [id_part] => 11
    )

[1] => Array
    (
        [lable] => label1
        [id_poste] => 2
        [id_part] => 12
    )

[2] => Array
    (
        [lable] => label2
        [id_poste] => 3
        [id_part] => 13
    )

[3] => Array
    (
        [lable] => label3
        [id_poste] => 4
        [id_part] => 14
    )

[4] => Array
    (
        [lable] => label0
        [id_poste] => 1
        [id_part] => 11
    )

[5] => Array
    (
        [lable] => label1_X
        [id_poste] => 2
        [id_part] => 12
    )

[6] => Array
    (
        [lable] => label2
        [id_poste] => 3
        [id_part] => 13
    )

[7] => Array
    (
        [lable] => label3_X
        [id_poste] => 4
        [id_part] => 14
    )

)

Like we see here the array is duplicated : So we using array_unique But we still got an error or warning .

Array to string conversion in C:\Program Files\EasyPHP-12.1\www\PHP\array_union.php on line 86

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
tree em
  • 20,379
  • 30
  • 92
  • 130

5 Answers5

6

I get a solution that is kind a tricky. I will merge A and its difference with B. To determine this difference, I use array_udiff: I think it's tricky because it relies on the identification with lable key.

$C = array_merge($A, 
            array_udiff($B, $A, 
                        function($a,$b){
                            return strcmp($a['lable'],$b['lable']);
                        }
           )
);
Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153
artragis
  • 3,677
  • 1
  • 18
  • 30
0
array_merge($A, $B);

is what you are looking for

Wing Lian
  • 2,387
  • 16
  • 14
0

use array_merge() for this purpose

$C = array_merge($A,$B);
echo '<pre>';
print_r($C);
echo '</pre>'

see working example http://codepad.viper-7.com/LrVHn3

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

You can use array_merge() for this.. Since you need to remove duplicates, u must also use array_unique()....

$C = array_unique(array_merge($A,$B), SORT_REGULAR);
echo '<pre>';
print_r($C);
echo '</pre>'

See array_unique and array_merge

It gives me correct result

Array
(
    [0] => Array
        (
            [lable] => label0
            [id_poste] => 1
            [id_part] => 11
        )

    [1] => Array
        (
            [lable] => label1
            [id_poste] => 2
            [id_part] => 12
        )

    [2] => Array
        (
            [lable] => label2
            [id_poste] => 3
            [id_part] => 13
        )

    [3] => Array
        (
            [lable] => label3
            [id_poste] => 4
            [id_part] => 14
        )

    [5] => Array
        (
            [lable] => label1_X
            [id_poste] => 2
            [id_part] => 12
        )

    [7] => Array
        (
            [lable] => label3_X
            [id_poste] => 4
            [id_part] => 14
        )

)
Ayyappan Sekar
  • 11,007
  • 2
  • 18
  • 22
  • Notice: Array to string conversion in C:\Program Files\EasyPHP-12.1\www\PHP\array_union.php on line 81 – tree em Dec 11 '12 at 07:17
0
<?php

$input = array_merge( $A, $B );
$input = array_map( "unserialize", array_unique(array_map("serialize", $input)) );

var_dump( $input );
Niclas Larsson
  • 1,317
  • 8
  • 13