I have a dynamically generated array of file names, let's say something like this:
$files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");
I have a few specific file names that I want to remove from the array:
$exclude_file_1 = "meta-file-1"; $exclude_file_2 = "meta-file-2";
So, I will always know the values ββof the elements that I want to reset, but not the keys.
I am currently considering a couple of ways to do this. One way using array_filter and a custom function:
function excludefiles($v) { if ($v === $GLOBALS['exclude_file_1'] || $v === $GLOBALS['exclude_file_2']) { return false; } return true; } $files = array_values(array_filter($files,"excludefiles"));
Another way, use array_keys and undo :
$exclude_files_keys = array(array_search($exclude_file_1,$files),array_search($exclude_file_2,$files)); foreach ($exclude_files_keys as $exclude_files_key) { unset($files[$exclude_files_key]); } $files = array_values($page_file_paths);
Both methods create the desired result.
I'm just wondering which one will be more efficient (and why)?
Or maybe there is another, more efficient way to do this?
How is there possibly a way to have multiple search values ββin an array_search function?
Feanne
source share