php arrays

WordPress is a fantastic framework, but its range of use delays some developments that would be possible by simply aligning the coding to the latest PHP versions and tools. Even though thinking of a full compatible WP release for PHP 8.0 is still a dream, most top plugins and the WP core are currently in line with PHP 7.3 and even 7.4, so the devs should be.

However, the fact is that some useful tools PHP currently offers for dealing with simple arrays have been overseen so far. Sometimes we catch pieces of code from Gists and Pastebins that use complex coding structures to perform operations that could be otherwise as simple as a single line or argument.

In WP, as a result, we face excessive queries that most times could be avoided, to grab the same data we already have in hands, but in a different format or disposition.

The spread operator

Forget about huge functions to flatten nested arrays. From the 5.3 version, WP core itself implemented the PHP spread operator (…) in a number of native functions. To illustrate how the operator works let’s consider a group of three related arrays:

$a = [1, 2, 3, 4];
$b = [$a, 5, [5.5, 5.7], 6, 7];
$c = [0, [$a, $b]];

/*
Result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
    [1] => 5
    [2] => Array
        (
            [0] => 5.5
            [1] => 5.7
        )
    [3] => 6
    [4] => 7
)

Array
(
    [0] => 0
    [1] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                )
            [1] => Array
                (
                    [0] => Array
                        (
                            [0] => 1
                            [1] => 2
                            [2] => 3
                            [3] => 4
                        )
                    [1] => 5
                    [2] => Array
                        (
                            [0] => 5.5
                            [1] => 5.7
                        )
                    [3] => 6
                    [4] => 7
                )
        )
)
*/

Of course, sometimes we simply want this nesting structure. However, in most cases we manipulate objects and look for specific array keys to retrive data. Many times, we can find plugin coding using structure like $variable[‘somekey’][‘subkey’][‘usefuldata’] to get the relevant data. But, is that really necessary?

Let’s use the same group of arrays, but now including a few spread operators.

$a = [1, 2, 3, 4];
$b = [...$a, 5, [5.5, 5.7], 6, 7];
$c = [0, [...$a, ...$b]];

/*
Result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => Array
        (
            [0] => 5.5
            [1] => 5.7
        )
    [6] => 6
    [7] => 7
)

Array
(
    [0] => 0
    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 1
            [5] => 2
            [6] => 3
            [7] => 4
            [8] => 5
            [9] => Array
                (
                    [0] => 5.5
                    [1] => 5.7
                )
            [10] => 6
            [11] => 7
        )
)
*/

The spread operator simply insert the values of the child array as simple elements in the new array. It’s not “flattening” exactly, but using the word, spreading the values of the first array within the second one.

But we can also apply the operator on the array elements, not just on variables. So if we want all of those arrays completely flattened, we could use:

$a = [1, 2, 3, 4];
$b = [...$a, 5, ...[5.5, 5.7], 6, 7];
$c = [0, ...[...$a, ...$b]];

/*
Result:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 5.5
    [6] => 5.7
    [7] => 6
    [8] => 7
)

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 1
    [6] => 2
    [7] => 3
    [8] => 4
    [9] => 5
    [10] => 5.5
    [11] => 5.7
    [12] => 6
    [13] => 7
)
*/

Interesting combinations

While PHP has roughly 50 native functions to deal with arrays, we keep strugling sometimes to perform operations that otherwise could be managed in a few lines. Some interesting results can be achieved by simply combining 2 or 3 of those functions.

Let’s consider the same arrays of the previous example. Now, we want the first two arrays to become a single one, completely flattened and with no duplicates in its values. All we need to do is using two different native functions:

$a = [1, 2, 3, 4];
$b = [...$a, 5, ...[5.5, 5.7], 6, 7];
$c = [0, ...[...$a, ...$b]];

$d = array_merge($a, $b);
/*
Result:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 1
    [5] => 2
    [6] => 3
    [7] => 4
    [8] => 5
    [9] => 5.5
    [10] => 5.7
    [11] => 6
    [12] => 7
)
*/
$e = array_unique($d);
/*
Result:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [8] => 5
    [9] => 5.5
    [10] => 5.7
    [11] => 6
    [12] => 7
)
*/

Modifying WP_Query results

We could spend hours with tonnes of examples of operations involving other array functions. However, the most important thing about this article is to bring them to the WP context. Uses are limitless, but the clearer way of showing how those PHP functions can be useful is the WP_Query results.

Let’s consider two different post queries, and look at their resulting post IDs. And then, how we can, in a simple way:

  • Getting all posts from both queries, but avoiding duplicates.
  • Getting just posts that appear in both queries.
  • Getting posts from the second query that were not in the first query.
$a = [101, 102, 113, 124]; /* 1st Query*/
$b = [101, 113, 123, 145, 167]; /* 2nd Query */

$d = array_intersect($a, $b);
/*
Result:
Array
(
    [0] => 101
    [2] => 113
)
*/
$d = array_unique(array_merge($a, $b));
/*
Result:
Array
(
    [0] => 101
    [1] => 102
    [2] => 113
    [3] => 124
    [6] => 123
    [7] => 145
    [8] => 167
)
*/
$d = array_diff($b, $a);
/*
Result:
Array
(
    [2] => 123
    [3] => 145
    [4] => 167
)
*/

Relatively simple examples, but they perfectly illustrate how useful PHP array functions can be. The same idea can be developed in many cases, especially to reduce the number and the complexity of queries and requests.

Before planning your next query, find all functions related to PHP arrays and analyze the possibility of manipulating the resulting arrays, rather than make sequential or successive queries and requests.