[英]How to unset array key if it contains certain string from search array?
My array print looks like this print_r($myArray); 我的数组打印看起来像这样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. 我需要找到样式名称2和5并取消设置它们,但我想仅通过它们的名称而不是完整的数组键来查找它们。 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. 我尝试了array_key_exists,但它只匹配确切的键值。 Thank you! 谢谢!
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]);
}
}
you could use the PHP in_array()
function for this. 您可以为此使用PHP in_array()
函数。
foreach($myArray as $path => $file) {
if(in_array(basename($path), $findInArray)) {
unset($myArray[$path]);
}
}
Use basename()
and in_array()
: 使用basename()
和in_array()
:
foreach($myArray as $path => $file ){
$filename = basename($path);
if (in_array($filename, $findInArray)) {
unset($myArray[$path]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.