string(20-6ren">
gpt4 book ai didi

php - 如果字符串包含任何字符,则从数组中删除元素

转载 作者:可可西里 更新时间:2023-11-01 00:21:34 26 4
gpt4 key购买 nike

如果字符串包含任何字符,则从数组中删除元素。例如,下面是实际的数组。

array(1390) {
[0]=>
string(9) "Rs.52.68""
[1]=>
string(20) ""php code generator""
[2]=>
string(9) ""Rs.1.29""
[3]=>
string(21) ""php codes for login""
[4]=>
string(10) ""Rs.70.23""

}

我需要这个数组来删除所有以 RS. 开头的元素。

预期结果

 array(1390) {
[0]=>
string(20) ""php code generator""
[1]=>
string(21) ""php codes for login""


}

到目前为止我尝试了什么:

foreach($arr as $ll)
{

if (strpos($ll,'RS.') !== false) {
echo 'unwanted element';
}

从上面的代码中,我如何从数组中删除不需要的元素。

最佳答案

您可以在 foreach 循环中获取 $key 并在数组上使用 unset():

foreach ($arr as $key => $ll) {
if (strpos($ll,'RS.') !== false) {
unset($arr[$key]);
}
}

请注意,这不会删除您的任何项目,因为“RS”永远不会出现。只有“卢比”。

关于php - 如果字符串包含任何字符,则从数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20035358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com