PHP Array Compare Merge, efficiency

Source: Internet
Author: User
PHP arrays

$arr 1 = Array (1, 3, 5, 9,34,128,129);
$arr 2 = Array (1, 3, 6, 22, 33, 128);
$arr 3 = array (128,78,56,33,5,1);

The desired result is:
$res = Array (1,128,3,5,33,9,34,129,6,22,78,56);

The rule is: Merge the 3 array, the highest repetition forward
$arr 1 $arr 2 $arr 3 must be desc or ASC.

If only $arr1 and $arr2 two, or a $arr4.
thanks~


Reply to discussion (solution)

$arr 1 = Array (1, 3, 5, 9,34,128,129), $arr 2 = Array (1, 3, 6, 22,33, +), $arr 3 = array (128,78,56,33,5,1); $t = Array_count_v Alues (Array_merge ($arr 1, $arr 2, $arr 3)), Arsort ($t);p Rint_r (Array_keys ($t));
Array
(
[0] = 1
[1] = 128
[2] = 5
[3] = 33
[4] = 3
[5] = 78
[6] = 56
[7] = 22
[8] = 129
[9] = 9
[Ten] + 34
[One] = 6
)
Only the interior function is fast, but it doesn't exactly match your requirements.
So I need to write some code myself.
$t = Array_count_values (Array_merge ($arr 1, $arr 2, $arr 3)), $res = Array (); for ($max =max ($t); $max >0; $max--)  $res = Array_merge ($res, Array_keys ($t, $max));p Rint_r ($res);
Array
(
[0] = 1
[1] = 128
[2] = 3
[3] = 5
[4] = 33
[5] = 9
[6] = 34
[7] = 129
[8] = 6
[9] = 22
[Ten] + 78
[One] = 56
)


Well, understand, or moderator to force AH ~ I also mechanically last night,
Array_diff not to force Ah, so so redundant

$arr 1 = Array (1,2,3,4,5,6,7,8,9,10), $arr 2 = Array (1,8,9,15,100), $arr 3 = Array (1,8,15,200), $res = Array (), $resHigh = AR Ray ();//height similar to $resmiddle = Array (),//$reslow = Array (),//Low $res = Array_merge ($arr 1, $arr 2, $arr 3); $res = My_array_unique ( $res);//exclude Duplicate $reshigh = Array_intersect ($arr 1, $arr 2, $arr 3);//Intersection $reslow = Array_merge (Array_diff ($arr 1, $arr 2, $arr 3), Array_diff ($arr 2, $arr 1, $arr 3), Array_diff ($arr 3, $arr 1, $arr 2));//combination difference Set $res = Array_diff ($res, $resHigh);//Exclude high$ Resmiddle = Array_diff ($res, $resLow);//Exclude Low$res = Array_merge ($resHigh, $resMiddle, $resLow); function My_array_   Unique ($array) {//exclude duplicate $out = Array ();        foreach ($array as $key = + $value) {if (!in_array ($value, $out)) {$out [$key] = $value; }} return $out;} echo "
";p Rint_r ($res); exit;/* (    [0] = 1    [1] = 8    [2] = 9    [3] =    [4] = 2    [5] = 3< C6/>[6] + 4    [7] = 5 [    8] = 6    [9] = 7    [Ten] =    [one] = [ten] = 200 )*/
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.