ZF-3529: Use + operator instead of array_merge to preserve numeric message ids and performance
Description
Your method Zend_Translate_Adapter_Array::_loadTranslationData use array_merge to add translation data:
$this->_translate[$locale] = array_merge($this->_translate[$locale], $data);
but this doesn't keep numeric array keys:
var_dump( array_merge( array("1"=>"one", "2" => "two"), array("3" => "three") ) );
-> array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
if you use the + operator this problem is fixed and the performance is optimized:
var_dump( array("3" => "three") + array("1"=>"one", "2" => "two") );
-> array(3) {
[3]=>
string(5) "three"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
}
this is a simple test script:
$array1 = array('a'=>'a', 'b'=>'b', 'c'=>'c', 'f' => 'wrong');
$array2 = array('d'=>'d', 'e'=>'e', 'f'=>'f');
$start = microtime(true);
for($i=0; $i<100000; $i++) {
$test1 = $array2 + $array1;
}
$end = microtime(true);
echo "+ : ".($end-$start)."\n";
$start = microtime(true);
for($i=0; $i<100000; $i++) {
$test2 = array_merge($array1, $array2);
}
$end = microtime(true);
echo "array_merge : ".($end-$start)."\n";
var_dump($test1);
var_dump($test2);
output:
+ : 0.17034506797791
array_merge : 1.0767459869385
array(6) {
["d"]=>
string(1) "d"
["e"]=>
string(1) "e"
["f"]=>
string(1) "f"
["a"]=>
string(1) "a"
["b"]=>
string(1) "b"
["c"]=>
string(1) "c"
}
array(6) {
["a"]=>
string(1) "a"
["b"]=>
string(1) "b"
["c"]=>
string(1) "c"
["f"]=>
string(1) "f"
["d"]=>
string(1) "d"
["e"]=>
string(1) "e"
}
Comments
Posted by Thomas Weidner (thomas) on 2008-06-27T15:01:02.000+0000
Note: Using integers as messageids is a absolute no-no and will lead to unexpected translations.
Posted by Thomas Weidner (thomas) on 2008-06-27T15:27:47.000+0000
Integrated with r9812
Posted by Thomas Weidner (thomas) on 2008-07-26T11:42:28.000+0000
Fixed within branch 1.5
Posted by Wil Sinclair (wil) on 2008-09-02T10:38:55.000+0000
Updating for the 1.6.0 release.