|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-07-08 20:38 UTC] php at maerdo dot com
Description:
------------
This page inform that the addition of two arrays add datas of left array to datas of the right array :
" L'opérateur + retourne le tableau de droite auquel sont ajoutés les éléments du tableau de gauche."
After test it, i note the behavior is opposite.
The addition of these arrays result in the addition of datas of the right array to datas of the left array.
<?php
$array1=['a'=>1,'b'=>2];
$array2=['a'=>'A','B','C'];
var_dump($array1+$array2);
// output
array(4) { ["a"]=> int(1) ["b"]=> int(2) [0]=> string(1) "B" [1]=> string(1) "C" }
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 15:00:01 2025 UTC |
The documentation appears to be correct from the English docs. I can't speak for the French translation, or its accuracy. However, I will note that the addition operator is left-associative (as PHP operators take on precedence and associativity rules mostly from C). So to say that the expression ($arr1 + $arr2) yields left association, is by definition correct. Because $arr1 will be appending all elements in $arr2, that belong to keys which do not exist in $arr1. In your example, the elements of $array1: ( 'a' => 1, 'b' => 2 ) show up first in the resulting array. Any keys found in $array2 that exist in $array1 (in this case 'a'), will simply be ignored and the values of keys found in the second operand, which do not exist in the first operand, will be appended in the yielding expression. This can be a bit confusing when phrased as "left is added to right" or "right is added to left". So instead it's best to explain this from the perspective of an operator as opposed to the perspective of the operands (in a technical documentation). A left associative operator tells us that the operands are grouped to the left in the absence of parenthesis. This means when we spot the operator we need to group its right-hand operand with its left-hand operand when applying the operator. I hope this might help to disambiguate the technical wording found in the French translation.