Get up to 80 % extra points for free! More info:

Lesson 6 - ArrayUtils library for working with arrays in PHP

In the previous lesson, Finishing StringUtils library for PHP, we finished the StringUtils library for PHP, which provided functionality used to work with strings. Now that we're done with strings, let's move on to the next important data structure in PHP, arrays. Similarly, we'll come up with several useful methods and gather them up into the ArrayUtils class.

ArrayUtils

First and foremost, let's create our class:

class ArrayUtils
{
}

Filtering by a key array

It often need to filter an array to make it only contain certain keys. If you use ICT.social's PDO wrapper, you know that we're able to insert a new row into a database table using the following code:

$user = array(
    'first_name' => 'John',
    'last_name' => 'Smith',
    'email' => '[email protected]',
);
Db::insert('user', $user);

The code could be used to register a new user. It would all still work if we passed the data from a form. However, we'd then be vulnerable to additional field injection into the form, which would later be added into the DB query. (e.g. "admin" = 1):

// This code is not safe
Db::insert('user', $_POST);

We need to filter the $_POST array so that it only contains the specified keys. The following approach is not limited only to $_POST, as we can use it on other arrays as well.

Add the following method to our class:

public static function filterKeys(array $input, array $keys)
{
    return array_intersect_key($input, array_flip($keys));
}

The filterKeys() method takes the input array and the array of keys we want to keep, as arguments. As for the actual code, we use the array_flip() function to get the array which has the keys that we want to keep and set them as its keys. Then, we make the key intersection using the array_intersec­t_key() function and return the result.

Let's demonstrate the usage of our new method on the original example where we securely got data from the $_POST array:

$keys = array('first_name', 'last_name', 'email');
Db::insert('user', ArrayUtils::filterKeys($_POST, $keys));

When there are lots of keys, it's useful to prefix them and then filter by said prefix.

public static function filterKeysPrefix($prefix, array $input)
{
    $output = array();
    foreach ($input as $key => $value)
    {
        if (mb_strpos($key, $prefix) === 0)
            $output[$key] = $value;
    }
    return $output;
}

The filterKeysPrefix() method accepts a prefix and an input array. Then, the resulting array will only have the keys which start with the $prefix string. The code is fairly straightforward.

Key-value pairs mapping

When retrieving data from the database, it'll come as an array of arrays. Sort of like this:

Array
(
    [0] => Array
        (
            [user_id] => 1
            [first_name] => John Smith
            [email] => [email protected]
        )

    [1] => Array
        (
            [user_id] => 2
            [first_name] => Jane Smith
            [email] => [email protected]
        )

    [2] => Array
        (
            [user_id] => 3
            [first_name] => James Brown
            [email] => [email protected]
        )
)

Mapping pairs

Now imagine that we needed to add a form to our application which has an HTML select element to allow the user to make a choice. For example, the label would be the user's name and the value being sent to the server would be their ID. If we used a form framework to do so, it's very likely that it would require the data to be in the key-value format:

Array
(
    [John Smith] => 1
    [Jane Smith] => 2
    [James Brown] => 3
)

Now, let's add the mapPairs() method to our class. This method will map the result of a database query to an associative array:

public static function mapPairs(array $rows, $keyKey, $valueKey)
{
    $pairs = array();
    foreach ($rows as $row)
    {
        $key = $row[$keyKey];
        // Key collision checking
        if (isset($pairs[$key]))
        {
            $i = 1;
            while (isset($pairs[$key . ' (' . $i . ')']))
            {
                $i++;
            }
            $key .= ' (' . $i . ')';
        }
        $pairs[$key] = $row[$valueKey];
    }
    return $pairs;
}

We pass an array to the method and the names of two of the keys. In the resulting array, the first key becomes a key, and the second key becomes a value.

Meaning that we'll get the array mentioned above using the following approach:

$users = Db::queryAll('SELECT first_name, user_id FROM user');
$items = ArrayUtils::mapPairs($users, 'first_name', 'user_id');

Notice that the method handles the case where two different users have the same name. If that were to happen, it numbers the items.

Mapping values

Similarly, we'll occasionally need to retrieve array values. For example, every one of the users' email addresses so as to send newsletters to them. In that case, we'd want the following resulting array:

Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
)

Here's the mapSingles() method, it isn't complicated at all:

public static function mapSingles(array $rows, $singleKey)
{
    $singles = array();
    foreach ($rows as $row)
    {
        $singles[] = $row[$singleKey];
    }
    return $singles;
}

Here's its usage:

$emails = ArrayUtils::mapSingles(Db::queryAll('SELECT email FROM user'), 'email');

In the next lesson, Finishing ArrayUtils library for PHP, we'll focus on working with prefixes, notation conversions, and XML import/export. With that, we'll finish up with this library.


 

Previous article
Finishing StringUtils library for PHP
All articles in this section
Libraries for PHP
Skip article
(not recommended)
Finishing ArrayUtils library for PHP
Article has been written for you by David Capka Hartinger
Avatar
User rating:
No one has rated this quite yet, be the first one!
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities