Since $starts and $ends depend solely on (which itself does not depend on ), you can move this logic the reducer function to avoid repeated checks for every element:
Yes, that line is perfectly valid and concise for what you want to do! It uses array_map with an arrow function (PHP 7.4+) to build the transformed array.
Just make sure $starts and $ends are defined before this line. Here's a minimal, complete snippet incorporating it:
array_map applies the function to each element of $data["ready_all_patterns"].
preg_quote($newpattern, '/') safely escapes regex special characters in $newpattern.
$starts and $ends wrap the escaped new pattern, preserving the delimiters and boundaries inferred from the original regex.
User
fn?
Assistant
Great question! In PHP, fn introduces an arrow function, which was added in PHP 7.4 as a shorter, more concise way to write anonymous functions (closures).
What is fn?
An arrow function is a shorthand for a simple closure, with implicit return and automatic capturing of variables from the surrounding scope.
To check if a variable is an array in PHP, you use the built-in function:
is_array($variable)
Example:
$data = [1, 2, 3];
if (is_array($data)) {
echo "It's an array!";
} else {
echo "Not an array.";
}
This will print:
It's an array!
Summary:
is_array($var) returns true if $var is an array.
Returns false otherwise.
Let me know if you want to check other types or more complex checks!
Your function changer mostly follows an imperative style; if you want it to be more functional, a few improvements could help. Functional programming emphasizes immutability and pure functions, so here are some suggestions:
What you have now:
You clone $headlines via serialize/unserialize to avoid side effects (mutating argument), which is one way to keep immutability.
You use a foreach loop, which is imperative.
You mutate $headlines["content"][$index]['content'] directly inside the loop.
To make it more functional:
Avoid serialize/unserialize for cloning — it's slow and potentially risky. Instead, if $headlines is a nested array, do a deep copy manually or ensure the function does not modify the input.
Use array_map instead of foreach — This avoids explicit looping and mutation.
Return a new transformed array instead of modifying input in-place.
Avoid relying on external variables ($patterns, $client) unless passed explicitly — for pure functions.
Example rewritten more functionally:
function changer($headlines, $patterns) {
// Assuming $headlines["content"] is an array of associative arrays
$client = 'reader'; // constant, could be parameterized if needed
// Map over the content array, transforming each item immutably
$newContent = array_map(function($item) use ($patterns, $client) {
[$newTitle, $newContentText] = array_values(
run_highlight($item['title'], $item['content'], $item['link'], $patterns, $client)
);
// Return new item with updated content (and title if you want to update it too)
return array_merge($item, ['content' => $newContentText, 'title' => $newTitle]);
}, $headlines['content']);
// Return a new headlines array with updated content
return array_merge($headlines, ['content' => $newContent]);
}
Benefits of this version:
Uses array_map for clearer functional transformation.
Returns a new $headlines array without modifying the input.
Avoids serialization cloning.
Explicit dependency injection of $patterns.
You can further improve purity by making run_highlight a pure function.
Summary
Your original function works, especially if cloning input is important. But to better align with functional style in PHP:
Use array functions (array_map, array_reduce)
Avoid mutation inside the function
Avoid serialize/unserialize cloning
If you want, I can help convert run_highlight or deal with deeper nested structures as well.
Whether you need to copy $headlines before processing depends on how you want to treat input mutability and what your calling code expects.
Key considerations:
If you want to avoid mutating the input array (immutability):
Yes, you should make a copy (deep copy if nested). This prevents side effects outside the function.
If you are OK with modifying the input array in place:
No copy is needed; just modify $headlines directly.
In PHP, arrays are copied on write
When you pass an array to a function, PHP makes a copy if you modify the array inside the function. So:
function changer($headlines) {
$headlines['foo'] = 'bar'; // this will not affect the original input outside if passed by value
return $headlines;
}
$input = ['content' => []];
$output = changer($input);
// $input remains unchanged because $headlines inside is a copy on write
But if you pass $headlinesby reference (function changer(&$headlines) { ... }), then modifications affect the original input — no copy is made.
What about nested arrays?
Because $headlines["content"] is a nested array, modifying $headlines["content"][0]['content'] also triggers copy-on-write for that nested array element. So even for nested arrays, modifications inside the function do not affect the original variable outside if passed by value.
So generally:
If $headlines is passed by value (normal function argument), you do not need to clone or deep-copy manually. Any modification inside your function creates a copy and will not affect the caller's data.
If you pass $headlinesby reference, and want to avoid mutating the caller’s data, then yes, you need to clone it manually.
This shows the original $original array is unchanged.
Conclusion:
You do not need to copy$headlines explicitly unless your function accepts it by reference or you want to pass back the same modified object and mutate the original.
The serialize/unserialize cloning in your original code can be removed unless you want a deep copy for some other reason.
Just write your function assuming inputs are passed by value (default in PHP) and modify and return the new version.
If you want help rewriting your function without the cloning step, just ask!
Yes! Making run_highlight a pure function can significantly improve the overall purity and predictability of your changer function.
What is a pure function?
A pure function is a function that:
Always returns the same output for the same inputs.
Has no side effects (does not modify any external state or variables).
Does not depend on or modify variables outside its scope.
Why is purity important?
Easier to test and reason about.
More predictable — no surprises from hidden state changes.
Better for functional programming styles, compositions, and debugging.