Reputation: 5023
My array print looks like this print_r($myArray);
Array
(
[http://link_to_the_file/stylename2.css] => Array
(
[mime] => text/css
[media] =>
[attribs] => Array
(
)
)
[http://link_to_the_file/stylename1.css] => Array
(
[mime] => text/css
[media] =>
[attribs] => Array
(
)
)
[http://link_to_the_file/stylename5.css] => Array
(
[mime] => text/css
[media] =>
[attribs] => Array
(
)
)
)
I need to find stylename 2 and 5 and unset them but I would like to find them only by their names and not the full array key. So I placed my search terms in array.
$findInArray = array('stylename2.css','stylename5.css');
foreach($myArray as $path => $file ){
// if $path contains a string from $findInArray
unset $myArray [$path];
}
What would be the best approach to this ? I tried array_key_exists but it matches only exact key value. Thank you!
Upvotes: 0
Views: 771
Reputation: 600
you could use the PHP in_array()
function for this.
foreach($myArray as $path => $file) {
if(in_array(basename($path), $findInArray)) {
unset($myArray[$path]);
}
}
Upvotes: 1
Reputation: 35973
try this:
$findInArray = array('stylename2.css','stylename5.css');
foreach($myArray as $path => $file ){
foreach($findInArray as $find){
if(strpos($path, $find) !== false)
unset($myArray[$path]);
}
}
Upvotes: 1
Reputation: 76656
Use basename()
and in_array()
:
foreach($myArray as $path => $file ){
$filename = basename($path);
if (in_array($filename, $findInArray)) {
unset($myArray[$path]);
}
}
Upvotes: 1