English 中文(简体)
PHP中阵列层联盟
原标题:Set Theory Union of arrays in PHP
  • 时间:2012-05-07 15:59:37
  •  标签:
  • php
  • arrays

目前有3个数学单元:交叉、差异和结合(统一)。 在PHP中,我们能够以阵列开展这一行动:

  • intersection: array_intersect
  • difference: array_diff

www.un.org/spanish/ecosoc 工会的职能是什么?

结果阵列中不得有重复(如array_intersection-array_diff)。

如果指数为数字,则array_joint http://www.php.net/manual/en/Function.array-joint.php“rel=“noretinger”>PHP docs。

最佳回答

Try array_joint:

array_unique(array_merge($array1, $array2));

PHP Manual

问题回答

array_unique(阵列_joint(......)>

被告方的回答是,从两个按顺序编号的阵列中得出一定数量的阵列——这里有一些选择,可以:

array_values(array_unique(array_merge($array1, $array2)));

(Adrien的答复,后重编钥匙)

array_keys(array_flip($array1)+array_flip($array2))

(确定钥匙中的数值,并使用阵列工会经营者)

array_merge($array1, array_diff($array2, $array1))

(Remove the shared values from the second array before merging)

Benchmark results (for merging two arrays of length 1000 a thousand times on my system):

  • Unique (Adrien s version): 2.862163066864 seconds
  • Values_Unique: 3.12 seconds
  • Keys_Flip: 2.34 seconds
  • Merge_Diff: 2.64 seconds

抽样测试,但两种阵列非常相似(至少80%重复):

  • Unique (Adrien s version): 2.92 seconds
  • Values_Unique: 3.15 seconds
  • Keys_Flip: 1.84 seconds
  • Merge_Diff: 2.36 seconds

It seems using the array union operator to do the actual union is the fastest method. Note however that array_flip is only safe if the array s values are all strings or all integers; if you have to produce the union of an array of objects, I recommend the version with array_merge and array_diff.

http://www.unique

http://php.net/manual/en/language.operators.array.php rel=“nofollow noreferer”>。 PHP: ArrayOperationtors 文件:

<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b
echo "Union of $a and $b: 
";
var_dump($c);

$c = $b + $a; // Union of $b and $a
echo "Union of $b and $a: 
";
var_dump($c);
?>

如执行,该书将打印如下:

Union of $a and $b:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
Union of $b and $a:
array(3) {
  ["a"]=>
  string(4) "pear"
  ["b"]=>
  string(10) "strawberry"
  ["c"]=>
  string(6) "cherry"
}

OP没有具体说明该联盟是按 值/strong>,还是通过key,而PHP则有,即合并价值和+合并钥匙。 成果取决于阵列是否是指数化还是主要为,这些阵列首先出现。

$a = [ a ,  b ];
$b = [ b ,  c ];

$c = [ a  =>  A ,   b  =>  B ];
‌‌$d = [ a  =>  AA ,  c  =>  C ];

Indexed array

array_joint

By value using array_merge

‌‌array_merge($a, $b); // [‌0 =>  a , 1 =>  b , 2 =>  b , 3 =>  c ]
‌‌array_merge($b, $a); // ‌[0 =>  b , 1 =>  c , 2 =>  a , 3 =>  b ]

merge by key using + operator

营运人

‌‌$a + $b; ‌// [0 =>  a , 1 =>  b ]
$b + $a; // [0 =>  b , 1 =>  c ]

Keyed array

By value using array_merge

‌array_merge($c, $d); // ‌[ a  =>  AA ,  b  =>  B ,  c  =>  C ]
array_merge($d, $c); // [ a  =>  A ,   c  =>  C ,  b  =>  B ]

merge by key using + operator

$c + $d; // [‌ a  =>  A ,   b  =>  B ,  c  =>  C ]
‌‌$d + $c; // ‌[ a  =>  AA ,  c  =>  C ,  b  =>  B ]

<代码>+的操作者:

$x[0] = 4;
$x[1] = 1;

$y[0] = 9;
$y[2] = 5;

$u = $y + $x;

// Results in:
$u[0] === 9;
$u[1] === 1;
$u[2] === 5;

注:$x +$y!=$y +$x

$result = array_merge_recursive($first, $second);

如果你有内部阵列阵列,则可能有用。

Just use $array1 + $array2 It will result union of both array.

引证:

function array_union($array1, $array2){
    return array_unique(array_merge($array1, $array2));
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...