PHP数组 - 删除元素有与钥匙启动 [英] PHP array - remove elements with keys that start with

查看:141
本文介绍了PHP数组 - 删除元素有与钥匙启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题 - 我试图删除与以'not__开始键的元素。
其内部laravel项目,所以我(可以)使用它的阵列功能。
我试图消除环路后的元素。这不删除任何东西,即它不工作:

 函数fixinput($ arrinput)
{
    $ keystoremove =阵列();
    的foreach($ arrinput为$关键=> $值);
    {
        如果(STARTS_WITH($键,'not__'))
        {
            $ keystoremove = array_add($ keystoremove,'',$键);
        }
    }
    $ arrinput = array_except($ arrinput,$ keystoremove);
    返回$ arrinput;
}

请注意,这将不会对阵列的唯一任务。我想试试自己。 :)

谢谢!


解决方案

  $ =过滤阵列();的foreach($数组$关键=> $值){
    如果(strpos($键,'not__')!== 0){
        $过滤[$关键] = $价值;
    }
}

Newbie question - I'm trying to remove elements with keys that start with 'not__'. Its inside laravel project so I (can) use its array functions. I'm trying to remove elements after the loop. This doesn't remove anything, i.e. it doesn't work:

function fixinput($arrinput)
{
    $keystoremove = array();
    foreach ($arrinput as $key => $value);
    {
        if (starts_with($key, 'not__'))
        {
            $keystoremove = array_add($keystoremove, '', $key);
        }
    }
    $arrinput = array_except($arrinput, $keystoremove);
    return $arrinput;
}

Note that it won't be the only task on array. I'll try that myself. :)

Thanks!

解决方案

$filtered = array();

foreach ($array as $key => $value) {
    if (strpos($key, 'not__') !== 0) {
        $filtered[$key] = $value;
    }
}

这篇关于PHP数组 - 删除元素有与钥匙启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆