You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, there's no ability to merge non-associative array values. In the example above, it's impossible for me to have the foo key contain ['foo', 'bar').
To give a more realistic example, I might have Data elements representing HTML tag attributes:
Add a third mode which acts more like PHP's array_merge() function - it will clobber things with string keys but append things with numeric keys.
To do this, we'll create three constants, one for each mode:
publicconstPRESERVE = 0; // equivalent to `false` in 1.xpublicconstREPLACE = 1; // equivalent to `true` in 1.x; default valuepublicconstMERGE = 2; // the new approach I'm proposing
We'd also rename $clobber to $mode since it's no longer all-or-nothing.
This approach provides some level of backward-compatibility for anyone not using strict types, as false gets coerced to 0 and true gets coerced to 1.
The text was updated successfully, but these errors were encountered:
Data::import()
,Data::importData()
, andUtil::mergeAssocArray()
support this notion of "clobbering" when trying to merge in two arrays. In a nutshell:$clobber
Valuefalse
true
For example:
However, there's no ability to merge non-associative array values. In the example above, it's impossible for me to have the
foo
key contain['foo', 'bar')
.To give a more realistic example, I might have
Data
elements representing HTML tag attributes:And I want to merge in some additional attributes:
In order to get this output:
Unfortunately neither
$clobber
mode allows this.Proposal
Add a third mode which acts more like PHP's
array_merge()
function - it will clobber things with string keys but append things with numeric keys.To do this, we'll create three constants, one for each mode:
We'd also rename
$clobber
to$mode
since it's no longer all-or-nothing.This approach provides some level of backward-compatibility for anyone not using strict types, as
false
gets coerced to0
andtrue
gets coerced to1
.The text was updated successfully, but these errors were encountered: