PHP - Perform parsing rules on nested array

Just loop it and do the regex on all subarrays:

$content = json_decode($json, true);

$options = [
    'pattern' => '/Trevor/i',
    'replacement' => 'Oliver',
];
$engine = new regexTextReplace($options);
foreach($content as $key => $v){
    $columns[$key] = $engine->apply($content, $key);
}
var_dump($columns);

Working demo:
https://3v4l.org/Pk2rC

The benefit of looping in the "PHP" side instead of in the class is that you can still apply the regex to only one or two of the subarrays.
If you loop in the class then you need to pass more arguments to restrict the looping or do some type of array slicing.


I would rewrite the apply function to loop over the entire table, processing each column if the column argument is not set, or if it matches the current table column:

public function apply(array $table, $column = false): array
{
    $out = array();
    foreach ($table as $col => $rows) {
        if ($column === false || $col == $column) {
            $out[$col] = array_map('self::regex_replace', $rows);
        }
        else {
            $out[$col] = $rows;
        }
    }
    return $out;
}

Demo on 3v4l.org


You could rewrite your apply method to this:

public function apply(array $table, $columns = false): array
{
    $columns = $columns === false ? array_keys($table) : (array)$columns;

    return array_map(function ($column) use ($table, $columns) {
      return in_array($column, $columns) ? array_map('self::regex_replace', $table[$column]) : $table[$column];
    }, array_keys($table));
}

You can pass either a single column, or an array of columns, or nothing (false) to specify the columns you want adjusted.

Demo: https://3v4l.org/Kn4FY