In PHP it's possible to do array + array. The 'plus' sign is a shorthand way of merging arrays, but there's a difference in how they are merged compared to using
Let's imagine these two arrays:
Merging them using
+ would result in the following:
Definition and Usage. The array function is used to create an array. In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index; Associative arrays - Arrays with named keys; Multidimensional arrays - Arrays containing one or more arrays. Isfloat - Finds whether the type of a variable is float isint - Find whether the type of a variable is integer isstring - Find whether the type of a variable is string isobject - Finds whether a variable is an object isarray - Finds whether a variable is an array. In PHP it's possible to do array + array. The 'plus' sign is a shorthand way of merging arrays, but there's a difference in how they are merged compared to using arraymerge. Let's imagine these two arrays. An array in PHP is actually an ordered map. A map is a type that associates values to keys.This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.
The PHP Interpreter. Contribute to php/php-src development by creating an account on GitHub.
array_merge, would result in this:
What's happening here is that
array_merge will override existing keys, while
+ will not. In other words: when a key exists in the first array,
+ will not merge an item with the same key from another array into the first one.
In our example, both arrays actually had numerical keys, like so:
Which explains why
$first + $second doesn't add 'c' as an element: there already is an item with index
0 in the original.
The same applies for textual keys:
+ also works with nested arrays:
+ will keep the
original value, while
Php Is Array Exist
One more thing to mention is that
+ will apply the same behaviour when merging multidimensional arrays.
Originally, this was called
is_list, but renamed due to the potential of naming conflicts with a potential list type.
If we do eventually end up with list/vec types, would the naming here conflict at all? Or would it cause confusion and name collision? (Insert name bikeshedding here.)
Php Is Array Key
There's definitely the potential for naming conflicts if the type is called
listbut not if it's called
varray similar to https://docs.hhvm.com/hack/built-in-types/arrays - I'd strongly prefer the latter if there was a viable implementation and it used sequential memory instead of a linked list.
If the type is named
list instead of
vec and ends up incompatible with arrays,there'd need to be an
$val is listor some other new type check with a less preferable name.If it's compatible with arrays/lists(e.g. only checked during property assignment, passing in arguments, and returning values), then it wouldn't be an issue.
Php Is Array And Not Empty
array_is_list(array $array) is consistent with many other
array_* methods, which only accept arrays.- It is very possible that we may end up using the word
list anyway despite those objections, because it's already a reserved keyword in PHP for unrelated syntax (
list($first, $second) = $values). Recently added types such as
iterable (and scalar types) were added in previous PHP versions despite not being reserved in the past.- The name
vector may conflict with the php-ds PECL depending on how functionality is implemented.
Providing objects with APIs similar to the external PECL https://www.php.net/manual/en/class.ds-vector.php and the SPL may be easier to adopt because it can be polyfilled,but there's the drawback that there aren't the memory savings from copy-on-write and that there's the performance overhead of method calls to offsetGet(), etc.
Php Is Array Empty
As mentioned in Changes to PHP's type system, I'd expect the addition of a separate/incompatible vector type to be a massive undertaking, and possibly unpopular if it splits the language.In Hack/HHVM, it was practical for users to adopt because HHVM is bundled with a typechecker that checks that the usesare correct at compile time - because PHP has no bundled type checker, a new type would potentially cause a lot of unintuitive behaviors.
Php Is Array Key
Additionally, a name of
is_list may cause confusion with built-in list types such as