-2

First of all thanks to Dave Chen for helping me with the merger.

Now I ran into a problem. When I merge using this code:

$string1 = "
;1
2
3
;4
";

$string2 = "
;1
;2
;3
;4
;5
;6
";

echo implode("\r\n",array_unique(array_merge(explode("\r\n",$string1),explode("\r\n",$string2))));

The output is ;1 2 3 ;4 ;2 ;3 ;5 ;6 but I need it to be ;1 2 3 ;4 ;5 ;6

4

1 回答 1

0

你可以试试 :

echo implode(" ", crazyMerge($string1, $string2));

输出

 ;1 2 3 ;4 ;5 ;6    

使用的功能

function crazyMerge($a, $b) {
    // Some Cleanup
    $a = array_map("trim", explode("\r\n", $a));
    $b = array_map("trim", explode("\r\n", $b));

    $ab = array();
    foreach($a as $v) {
        if (! in_array(ltrim($v, ";"), $ab) && ! in_array($v, $ab))
            $ab[] = $v;
    }
    foreach($b as $k => $v) {
        if (! in_array(ltrim($v, ";"), $ab) && ! in_array($v, $ab))
            $ab[] = $v;
    }
    return $ab;
}
于 2013-05-06T01:17:35.667 回答