Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magnetikonline/f47d56399e54860945ac to your computer and use it in GitHub Desktop.
Save magnetikonline/f47d56399e54860945ac to your computer and use it in GitHub Desktop.
PHP array addition examples.

PHP array addition

Example script

<?php
$first = [
	'one' => 'first',
	'two' => 'first',
	'three' => 'first'
];

$second = [
	'one' => 'second',
	'two' => 'second',
	'four' => 'second'
];

$third = [1,2,3,4];
$fourth = [5,6,7,8];

var_dump($first + $second);
var_dump($second + $first);
var_dump($third + $fourth);

Output

array(4) {
  ["one"]=>
  string(5) "first"
  ["two"]=>
  string(5) "first"
  ["three"]=>
  string(5) "first"
  ["four"]=>
  string(6) "second"
}
array(4) {
  ["one"]=>
  string(6) "second"
  ["two"]=>
  string(6) "second"
  ["four"]=>
  string(6) "second"
  ["three"]=>
  string(5) "first"
}
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment