Published on

Efficiently Filtering and Formatting Arrays in PHP: A Drupal Paragraph Use Case

Authors

When working with multidimensional arrays in PHP, especially in the context of Drupal, you might face the challenge of filtering out null values from fields derived from a Drupal Paragraph entity. This guide will show you how to efficiently filter and format such data, ensuring that only the meaningful content remains.

The Challenge

Imagine you have an array of data where some fields come from a Drupal Paragraph entity. Some of these fields might be null, and you want to filter out any fields where all values are null. Here’s an example:

// Input array
$data = [
  ['class' => 'A', 'field_1' => 1, 'field_2' => null],
  ['class' => 'B', 'field_1' => 2, 'field_2' => null],
  ['class' => 'C', 'field_1' => 3, 'field_2' => null]
];

In this example, field_2 is null for all entries. Our goal is to remove field_2 from the final result and only keep fields with actual data.

The Solution

We can solve this by iterating over the data, building a result array, and then filtering out keys with only null values.

Step 1: Initialize the Result Array

Start by initializing a result array with the expected keys:

$result = [
  'class' => [],
  'field1' => [],
  'field2' => []
];

Step 2: Populate the Result Array

Next, populate this array with values from the input data:

foreach ($data as $item) {
  $result['class'][] = $item['class'];
  $result['field1'][] = $item['field_1'];
  $result['field2'][] = $item['field_2'];
}

Step 3: Filter Out Null Values

Filter out keys where all values are null:

foreach ($result as $key => $values) {
  if (array_filter($values, fn($value) => $value !== null) === []) {
    unset($result[$key]);
  }
}

This code uses array_filter with a callback to remove keys with all null values from the result array.

Step 4: Output the Results

Finally, output the filtered data in a readable format:

foreach ($result as $key => $values) {
  echo $key . ' ' . implode(' ', $values) . "\n";
}

This will produce:

class A B C
field1 1 2 3

Conclusion

By following these steps, you can efficiently filter and format multidimensional arrays in PHP, especially when dealing with Drupal Paragraph fields. This method ensures that only relevant data is processed and displayed, making your code concise and efficient. The key takeaway is that filtering and formatting can be done in a single pass, optimizing both performance and readability.

For more coding tips and tutorials, check out other articles on NC's Blog.