Skip to content

Instantly share code, notes, and snippets.

@srgoogleguy
Last active April 19, 2023 06:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save srgoogleguy/214db6389024e9125bcb1f43804cc2e4 to your computer and use it in GitHub Desktop.
Save srgoogleguy/214db6389024e9125bcb1f43804cc2e4 to your computer and use it in GitHub Desktop.
Array Visualizations in PHP

Visualizing Array Operations in PHP

It's important to recognize that in PHP an array does not always behave like a traditional array. It's actually more representative of an ordered hashmap. Thus it maintains order, but consists of key/value pairs. A key, in a PHP array, is always unique, but can be either an integer or a string. If the string key can be cast to an integer then PHP will carry out this operation implicitly. Also, array keys do not determine the order of elements in a PHP array.

With this in mind, we can better understand how different operations, like diff, intersect, union, and merge, would be carried out on a native PHP array. Some of these operations rely on the array keys and others on the values in the array.


Table of Contents

  1. Diff
  2. Intersect
  3. Union
  4. Merge

1. Diff

The diff of two or more arrays in PHP using array_diff(), relies on the values of the array to create a diff. The diff is made up of all values in the left-most argument of the function, that do not exist in any of the other arguments.

You can also conduct this same operation on the array keys, as opposed to their values, using array_diff_key().

1.1 Example

$array1 = [
    "red",
    2,
    "green",
    "blue",
];

$array2 = [
    "orange",
    "green",
    "blue",
    "red",
];

$diff = array_diff($array1, $array2);

var_dump($diff);

1.2 Output

array(1) {
  [1]=>
  int(2)
}

1.3 Visualization

In depicting the visualization of an array diff, we can see that only the values from the highlighted area of the venn diagram are retained in the $diff resulting array. Whereas, all other values are droppped.

Array Diff

2. Intersect

An intersection, unlike a diff, finds all values in the left-most argument of the function array_intersect() that also exist in all of the other arguments. Again, this same operation can be conduct on the keys instead of the values by using array_intersect_key().

2.1 Example

$array1 = [
    "red",
    2,
    "green",
    "blue",
];

$array2 = [
    "orange",
    "green",
    "blue",
    "red",
];

$diff = array_intersect($array1, $array2);

var_dump($diff);

2.2 Output

array(3) {
  [0]=>
  string(3) "red"
  [2]=>
  string(5) "green"
  [3]=>
  string(4) "blue"
}

2.3 Visualization

As you can see from this visualization, the highlighted areas of an intersect are only those values shared between all intersecting arrays. The same concept would apply for array_intersect_key() except the keys would be placed in the venn diagram, instead of the values of each array, and only those keys that are common amongst all arrays are used.

Array Intersect

3. Union

In PHP, a union of two or more arrays, is conducted strictly on the array keys and not their values. This is because a union is basically the set of array keys from the left-hand operand, combined with check-and-set operation of the right-hand operand's keys. If the key from the right-hand operand does not exist in the left-hand operand, it (along with its value) will be appeneded to the left-hand operand. Otherwise, it is ignored.

3.1 Example

$array1 = [
    0 => "first",
    1 => "second",
    2 => "third",
    3 => "fourth",
];

$array2 = [
    0 => "fifth",
    5 => "sixth",
    3 => "seventh",
    4 => "eighth",
];

$union = $array1 + $array2;

var_dump($union);

3.2 Output

array(6) {
  [0]=>
  string(5) "first"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "third"
  [3]=>
  string(6) "fourth"
  [5]=>
  string(5) "sixth"
  [4]=>
  string(6) "eighth"
}

3.3 Visualization

The union basically takes only keys from the first array and those keys which are not shared in the remaining arrays (i.e. nothing that shows up in the unhighlighted intersection of the venn diagram) as the resulting $union array.

So basically an array union is the exact inverse of an array intersection (on the array keys) with the operands reversed in order. If you were to do var_dump(array_intersect_key($array2, $array1)) on these operands you'd get exactly what you see in the unhighlighted intersection of this venn diagram.

Array Union

NOTE

This venn diagram uses the array keys (as opposed to its values) to illustrate the sets, subsets, and intersecting sets of these arrays.

4. Merge

A merger of two or more arrays in PHP results in taking the values of all integer keys in all arrays, and pushing them onto a new array. The values of string keys are then set, retaining the same keys. Thus right-hand operands overwrite any string keys that also exist in left-hand operands, respectively.

4.1 Example

$array1 = [
    0 => "first",
    1 => "second",
    2 => "third",
    3 => "fourth",
    "foo" => 'This is foo from $array1',
];

$array2 = [
    0 => "fifth",
    5 => "sixth",
    3 => "seventh",
    4 => "eighth",
    "foo" => 'This is foo from $array2',
];

$merge = array_merge($array1, $array2);

var_dump($merge);

4.2 Output

array(9) {
  [0]=>
  string(5) "first"
  [1]=>
  string(6) "second"
  [2]=>
  string(5) "third"
  [3]=>
  string(6) "fourth"
  ["foo"]=>
  string(24) "This is foo from $array2"
  [4]=>
  string(5) "fifth"
  [5]=>
  string(5) "sixth"
  [6]=>
  string(7) "seventh"
  [7]=>
  string(6) "eighth"
}

4.3 Visualization

As you can see from the merge visualization, we get all values, with the exception of intersecting string keys overwriting each other successively. In this case notice that $merge["foo"] has a value of "This is foo from $array2" instead of "This is foo from $array1", which is unlike the behavior of a union where the first value would be retained rather than overwriting it with the second.

Array Union

NOTE

This venn diagram uses the array keys (as opposed to its values) to illustrate the sets, subsets, and intersecting sets of these arrays.

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