[TYPO3-dev] new array operations :-)

Jigal van Hemert jigal at xs4all.nl
Mon Mar 15 20:47:31 CET 2010


Steffen Kamper wrote:
> never saw that, so i want to share
> 
> $a = array(
>     'a' => "Apfel",
>     'b' => "Banane",
> );
> 
> $b = array(
>     'a' => 'Anker',
>     's' => "Sonne",
>     'm' => "Meer",
> );
> echo '<pre>';
> print_r($a + $b);
> print_r(array_merge($a, $b));
> echo '</pre>';
> 
> result:
> Array
> (
>     [a] => Apfel
>     [b] => Banane
>     [s] => Sonne
>     [m] => Meer
> )
> Array
> (
>     [a] => Anker
>     [b] => Banane
>     [s] => Sonne
>     [m] => Meer
> )
> 
> 
> so $a + $b is same as array_merge, but first array wins, with 
> array_merge second wins.

There are other differences:
array_merge() with numeric keys appends the elements from the second 
array to the first and reindexes the numeric keys.

Even funnier:
t3lib_div::array_merge($a, $b) is implemented as.... $b + $a
So this acts like array_merge() except that the numeric keys are not 
appended and the numeric keys are not reindexed.

And finally:
t3lib_div::array_merge_recursive_overrule($a, $b, $notAddKeys, 
$includeEmtpyValues)
Here the second array wins. With $notAddKeys set it will not add 
elements from $b which are not present in $a (only overwriting existing 
keys); $includeEmptyValues when set will also use values which evaluate 
to false.

> What a pity that $a - $b is not supported, that would be consequent.
The only effect I could think of is the elements from $a which are not 
present in $b. And this is exactly what array_diff() does. array_diff() 
accepts more arguments and acts like $a - $b - $c - ...

Any suggestions for:

$a * $b   (Matrix-product?)

$a / $b   (intersect?)

$a % $b

Always fun stuff, arrays ;-)

-- 
Jigal van Hemert.




More information about the TYPO3-dev mailing list