Zend Framework

Use + operator instead of array_merge to preserve numeric message ids and performance

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.2
  • Fix Version/s: 1.6.0
  • Component/s: Zend_Translate
  • Labels:
    None

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"
}

Activity

Hide
Thomas Weidner added a comment -

Note:
Using integers as messageids is a absolute no-no and will lead to unexpected translations.

Show
Thomas Weidner added a comment - Note: Using integers as messageids is a absolute no-no and will lead to unexpected translations.
Hide
Thomas Weidner added a comment -

Integrated with r9812

Show
Thomas Weidner added a comment - Integrated with r9812
Hide
Thomas Weidner added a comment -

Fixed within branch 1.5

Show
Thomas Weidner added a comment - Fixed within branch 1.5
Hide
Wil Sinclair added a comment -

Updating for the 1.6.0 release.

Show
Wil Sinclair added a comment - Updating for the 1.6.0 release.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
Not Specified
Original Estimate - Not Specified
Remaining:
0m
Remaining Estimate - 0 minutes
Logged:
3h
Time Spent - 3 hours