How do I filter/hide an element by some specific extension from an array when I know the element's value? for example: I have an array:
$rearray = array('.ws_config', 'Akms-wpp-all-vars.cfg', 'makes-lop-use-vars.cfg', '.ptp-sync/ASERD_EDITMSG', '.ptp-sync/index', '.ptp-sync/hooks/apply_value');
I want to Filter all the .ptp-sync
values and display the rest value except (.ptp-sync
)
New in programming, I'm looking for the simplest function to perform this task, please.
My scripts -
$rearray = array('.ws_config', 'gtdp-dxc-all-vars.cfg', 'gtdp-dxc-usr-vars.cfg', '.ptp-sync/COMMIT_EDITMSG', '.ptp-sync/index', '.ptp-sync/hooks/applypatch-msg.sample');
$array_without_sync = array_diff($rearray, array('.ptp-sync'));
print_r($array_without_sync);
My output display all values except (.ptp-sync)
Thank you
CodePudding user response:
You can use a function to decide what is to be kept or not.
function keepelement($value) {
if (!preg_match('/\.ptp-sync/',$value)) return true;
return false;
}
$rearray = array('.ws_config', 'gtdp-dxc-all-vars.cfg', 'gtdp-dxc-usr-vars.cfg', '.ptp-sync/COMMIT_EDITMSG', '.ptp-sync/index', '.ptp-sync/hooks/applypatch-msg.sample');
$array_without_sync = array_filter($rearray, "keepelement", NULL);
print_r($array_without_sync);
Output
Array
(
[0] => .ws_config
[1] => gtdp-dxc-all-vars.cfg
[2] => gtdp-dxc-usr-vars.cfg
)
CodePudding user response:
The simplest function is strpos(php)
giving you the zero-based offset of the second parameter inside the first parameter (both string) as integer or false if not found.
You can express your condition (predicate) by making it a function itself that returns bool:
$predicate = fn($s) => 0 !== strpos($s, '.ptp-sync/');
This then is perhaps the simplest form on how to write the expression in PHP.
With array_filter(php)
you can apply the predicate on your array, it is the simplest function in PHP to filter an array.
It filters the array in the first parameter by the predicate in the second parameter that is a bool returning function called for each value in the array_filter()
first parameter array as the predicates function first parameter.
Example of array_filter(php)
with predicate:
$result = array_filter($array, $predicate);
For the how to hide part, e.g. not within a foreach
iteration, the predicate can be implemented in FilterIterator::accept(php)
, it is built-in within PHP so this solution is similar ready-made:
$hidden = new class (new ArrayIterator($array)) extends FilterIterator {
public function accept(): bool
{
return 0 !== strpos($this->current(), '.ptp-sync/');
}
};
If you consider regular expressions simple, the function that allows you to filter the array in one go is preg_grep(php)
taking the regular expression pattern as its first parameter - string -, the array as the second parameter and PREG_GREP_INVERT
as flags in the third parameter, allowing to turn match into no-match which often makes it more easy to formulate the predicate condition as a regular expression pattern.
The regular expression pattern needs to be formulated first, similar to the predicate
with strpos(php)
above:
~^\Q.ptp-sync/\E~
(anchored at the beginning (^), the verbatim string (\Q...\E) ".php-sync/")
$pattern = '~^\Q.ptp-sync/\E~';
$result = preg_grep($pattern, $array, PREG_GREP_INVERT);