Union of array values in PHP

The array union operator ($a + $b) calculates a union using array keys. An efficient method for achieving a union of array values follows.

The union of two sets is all the elements from both sets, with duplicates removed. The array_merge( ) and array_unique( ) functions let you calculate the union.

Anonymous Objects in PHP

For real quick and dirty one-liner anonymous objects, just cast an associative array:

= (object) array('foo' => 'bar', 'property' => 'value');

$obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'

... no need to create a new class or function to accomplish it.


Syndicate content