ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ для Ρ€Π°Π±ΠΎΡ‚Ρ‹ с массивами

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€Ρ‹ массивов
ΠŸΡ€ΠΈΠΌΠ΅Ρ€ НазваниС Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚
$a + $b ОбъСдинСниС ОбъСдинСниС массива $a с массивом $b.
$a == $b Π Π°Π²Π½ΠΎ Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $a ΠΈ массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $b содСрТат ΠΎΠ΄Π½ΠΈ ΠΈ Ρ‚Π΅ ΠΆΠ΅ ΠΏΠ°Ρ€Ρ‹ Β«ΠΊΠ»ΡŽΡ‡ β€” Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅Β».
$a === $b ВоТдСствСнно Ρ€Π°Π²Π½ΠΎ Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $a ΠΈ массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $b содСрТат ΠΎΠ΄Π½ΠΈ ΠΈ Ρ‚Π΅ ΠΆΠ΅ ΠΏΠ°Ρ€Ρ‹ Β«ΠΊΠ»ΡŽΡ‡ β€” Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅Β» Π² Ρ‚ΠΎΠΌ ΠΆΠ΅ порядкС ΠΈ Ρ‚ΠΎΠ³ΠΎ ΠΆΠ΅ Ρ‚ΠΈΠΏΠ°.
$a != $b НС Ρ€Π°Π²Π½ΠΎ Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $a Π½Π΅ Ρ€Π°Π²Π΅Π½ массиву Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $b.
$a <> $b НС Ρ€Π°Π²Π½ΠΎ Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $a Π½Π΅ Ρ€Π°Π²Π΅Π½ массиву Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $b.
$a !== $b ВоТдСствСнно Π½Π΅ Ρ€Π°Π²Π½ΠΎ Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли массив Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $a Π½Π΅ Ρ€Π°Π²Π΅Π½ тоТдСствСнно массиву Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ $b.

ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ + Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Π»Π΅Π²Ρ‹ΠΉ массив, ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ присоСдинился ΠΏΡ€Π°Π²Ρ‹ΠΉ массив. Для ΠΊΠ»ΡŽΡ‡Π΅ΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ содСрТатся Π² ΠΎΠ±ΠΎΠΈΡ… массивах, Π²Ρ‹Π±ΠΈΡ€Π°ΡŽΡ‚ΡΡ значСния ΠΈΠ· Π»Π΅Π²ΠΎΠ³ΠΎ массива, Π° элСмСнты ΠΈΠ· ΠΏΡ€Π°Π²ΠΎΠ³ΠΎ массива, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΠΈΠΌ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‚, ΠΈΠ³Π½ΠΎΡ€ΠΈΡ€ΡƒΡŽΡ‚ΡΡ.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠžΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ добавлСния ΠΎΠ΄Π½ΠΎΠ³ΠΎ массива Π² ΠΊΠΎΠ½Π΅Ρ† Π΄Ρ€ΡƒΠ³ΠΎΠ³ΠΎ

<?php

$a
= array("a" => "яблоко", "b" => "банан");
$b = array("a" => "Π³Ρ€ΡƒΡˆΠ°", "b" => "ΠΊΠ»ΡƒΠ±Π½ΠΈΠΊΠ°", "c" => "вишня");

$c = $a + $b; // ОбъСдинСниС массивов $a и $b
echo "ОбъСдинСниС массивов \$a и \$b:\n";
var_dump($c);

$c = $b + $a; // ОбъСдинСниС массивов $b и $a
echo "ОбъСдинСниС массивов \$b и \$a:\n";
var_dump($c);

$a += $b; // Π’Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $a += $b ΠΎΠ±ΡŠΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ‚ массивы $a ΠΈ $b
echo "ОбъСдинСниС массивов Π²Ρ‹Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ \$a += \$b:\n";
var_dump($a);

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

ОбъСдинСниС массивов $a и $b:
array(3) {
  ["a"]=>
  string(12) "яблоко"
  ["b"]=>
  string(10) "Π±Π°Π½Π°Π½"
  ["c"]=>
  string(10) "вишня"
}
ОбъСдинСниС массивов $b и $a:
array(3) {
  ["a"]=>
  string(10) "Π³Ρ€ΡƒΡˆΠ°"
  ["b"]=>
  string(16) "ΠΊΠ»ΡƒΠ±Π½ΠΈΠΊΠ°"
  ["c"]=>
  string(10) "вишня"
}
ОбъСдинСниС массивов $a += $b:
array(3) {
  ["a"]=>
  string(12) "яблоко"
  ["b"]=>
  string(10) "Π±Π°Π½Π°Π½"
  ["c"]=>
  string(10) "вишня"
}

ΠŸΡ€ΠΈ сравнСнии элСмСнты массива ΠΏΡ€ΠΈΠ·Π½Π°ΡŽΡ‚ΡΡ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ‡Π½Ρ‹ΠΌΠΈ, Ссли совпадаСт ΠΈ ΠΊΠ»ΡŽΡ‡, ΠΈ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΊΠ»ΡŽΡ‡Π°.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 Π‘Ρ€Π°Π²Π½Π΅Π½ΠΈΠ΅ массивов

<?php

$a
= array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");

var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 7 notes

up
240
cb at netalyst dot com ΒΆ
17 years ago
The union operator did not behave as I thought it would on first glance. It implements a union (of sorts) based on the keys of the array, not on the values.

For instance:
<?php
$a = array('one','two');
$b=array('three','four','five');

//not a union of arrays' values
echo '$a + $b : ';
print_r ($a + $b);

//a union of arrays' values
echo "array_unique(array_merge($a,$b)):";
// cribbed from http://oreilly.com/catalog/progphp/chapter/ch05.html
print_r (array_unique(array_merge($a,$b)));
?>

//output

$a + $b : Array
(
    [0] => one
    [1] => two
    [2] => five
)
array_unique(array_merge(Array,Array)):Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
)
up
43
Q1712 at online dot ms ΒΆ
19 years ago
The example may get u into thinking that the identical operator returns true because the key of apple is a string but that is not the case, cause if a string array key is the standart representation of a integer it's gets a numeral key automaticly. 

The identical operator just requires that the keys are in the same order in both arrays:

<?php
$a = array (0 => "apple", 1 => "banana");
$b = array (1 => "banana", 0 => "apple");

var_dump($a === $b); // prints bool(false) as well

$b = array ("0" => "apple", "1" => "banana");

var_dump($a === $b); // prints bool(true)
?>
up
26
dfranklin at fen dot com ΒΆ
22 years ago
Note that + will not renumber numeric array keys.  If you have two numeric arrays, and their indices overlap, + will use the first array's values for each numeric key, adding the 2nd array's values only where the first doesn't already have a value for that index.  Example:

$a = array('red', 'orange');
$b = array('yellow', 'green', 'blue');
$both = $a + $b;
var_dump($both);

Produces the output:

array(3) { [0]=>  string(3) "red" [1]=>  string(6) "orange" [2]=>  string(4) "blue" }

To get a 5-element array, use array_merge.

    Dan
up
21
amirlaher AT yahoo DOT co SPOT uk ΒΆ
23 years ago
[]= could be considered an Array Operator (in the same way that .= is a String Operator). 
[]= pushes an element onto the end of an array, similar to array_push:
<? 
  $array= array(0=>"Amir",1=>"needs");
  $array[]= "job";
  print_r($array);
?>
Prints: Array ( [0] => Amir [1] => needs [2] => job )
up
8
xtpeqii at Hotmail dot com ΒΆ
8 years ago
$a=[ 3, 2, 1];
$b=[ 6, 5, 4];
var_dump( $a + $b );

output:
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(2)
  [2]=>
  int(1)
}

The reason for the above output is that EVERY array in PHP is an associative one.  
Since the 3 elements in $b have the same keys( or numeric indices ) as those in $a, those elements in $b are ignored by the union operator.
up
13
Dan Patrick ΒΆ
14 years ago
It should be mentioned that the array union operator functions almost identically to array_replace with the exception that precedence of arguments is reversed.
up
1
Anonymous ΒΆ
3 years ago
Merge two arrays and retain only unique values.
Append values from second array.
Do not care about keys.

<?php
$array1 = [
    0 => 'apple',
    1 => 'orange',
    2 => 'pear',
];

$array2 = [
    0 => 'melon',
    1 => 'orange',
    2 => 'banana',
];

$result = array_keys(
    array_flip($array1) + array_flip($array2)
);
?>

Result:
[
  [0] => "apple",
  [1] => "orange",
  [2] => "pear",
  [3] => "melon",
  [4] => "banana",
}