Issues

ZF-6459: The Array-Union-Operator ignores values of existing keys

Issue Type: Bug Created: 2009-04-28T23:41:53.000+0000 Last Updated: 2009-05-15T12:02:07.000+0000 Status: Resolved Fix version(s): - 1.8.1 (12/May/09)

Reporter: Sebastian Krebs (kingcrunch) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Application

Related issues: - ZF-6679

Attachments:

Description

The union operator found in

<pre class="highlight">
    public function setOptions(array $options)
// ..
        $this->_options = $this->_options + $options;
        return $this;
    }

ignores subkeys, if the key at toplevel already exists.

<pre class="highlight">$a = array ('config'=>array('key1'=>'$a1','key2'=>'$a2'));
$b = array ('config'=>array('key1'=>'$b1','key3'=>'$b2'));
var_dump ($a + $b);

array(1) {
  ["config"]=>
  array(2) {
    ["key1"]=>
    string(3) "$a1"
    ["key2"]=>
    string(3) "$a2"
  }
}

The 'key3' is missing. This behaviour is described in the PHP-Manual [1]

'array_merge' does the same in the other direction, it overwrite the value of a existing key, instead of ignoring it. 'array_merge_recursive' will merge two scalar to one array and so it will destroy the config structure. Zend_Config has a method merge() which act like expected, but its not used here. So the method 'setOptions()' has to convert one config into a 'Zend_Config', merge and deconvert into an array again, or it must implement its own merge-Method.

Version is trunk 15243

[1]http://de.php.net/manual/en/… {quote} The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten. ```

Comments

Posted by Matthew Weier O'Phinney (matthew) on 2009-05-07T06:23:33.000+0000

Resolved in trunk and 1.8 release branch

Posted by Sebastian Krebs (kingcrunch) on 2009-05-11T18:39:36.000+0000

As i mentioned 'array_merge_recursive' will lead to another problem

<pre class="highlight">$a = array (
  'config'=>array(
    'db'=>array(
      'dbname'=>'name')));
$b = array (
  'config'=>array(
    'db'=>array(
      'dbname'=>'thisConfigUseAnotherName')));

var_dump (array_merge_recursive($a,$b));

output

<pre class="highlight">array(1) {
  ["config"]=>
  array(1) {
    ["db"]=>
    array(1) {
      ["dbname"]=>
      array(2) {
        [0]=>
        string(4) "name"
        [1]=>
        string(22) "thisConfigUseOtherName"
      }
    }
  }
}

Now "dbname" is not a string anymore and will likely fail.

Another example, which returns a very iteresting result.

<pre class="highlight">class Test_Bootstrap extends Zend_Application_Module_Bootstrap {
    protected function _initOptionDump () {
        $this->setOptions($this->getOptions());
        var_dump ($this->getOptions());
    }
}


<pre class="highlight">
All scalar values are doubled.

array(1) { ["somekey"]=> array(2) { [0]=> string(9) "somevalue" [1]=> string(9) "somevalue" } }

<pre class="highlight">
Expected: No change.

array(1) { ["somekey"]=> string(9) "somevalue" } ```

Posted by Matthew Weier O'Phinney (matthew) on 2009-05-12T07:45:45.000+0000

Better merging algorithm added in trunk and 1.8 release branch.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts