如何从 Array 中删除特定键?PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5202068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:36:18  来源:igfitidea点击:

How do I remove specific keys from Array? PHP

phparrayskey

提问by user585303

I am trying to remove keys from array.

我正在尝试从数组中删除键。

Here is what i get from printing print_r($cats);

这是我从打印 print_r($cats) 中得到的;

Array
(
    [0] => <a href="/website/index.php/Category:All" title="Category:All">All</a> &gt; <a href="/website/index.php/Category:Computer_errors" title="Category:Computer errors">Computer errors</a> &gt; <a href="/website/index.php/Category:HTTP_status_codes" title="Category:HTTP status codes">HTTP status codes</a> &gt; <a href="/website/index.php/Category:Internet_terminology" title="Category:Internet terminology">Internet terminology</a> 
    [1] => 
<a href="/website/index.php/Category:Main" title="Category:Main">Main</a> 
)

I am trying to use this to remove "Main" category from the array

我正在尝试使用它从数组中删除“主要”类别

    function array_cleanup( $array, $todelete )
{
    foreach( $array as $key )
    {
        if ( in_array( $key[ 'Main' ], $todelete ) )
        unset( $array[ $key ] );

    }
    return $array;
}

$newarray = array_cleanup( $cats, array('Main') );

Just FYI I am new to PHP... obviously i see that i made mistakes, but i already tried out many things and they just don't seem to work

仅供参考,我是 PHP 新手...显然我看到我犯了错误,但我已经尝试了很多东西,但它们似乎不起作用

回答by Gaurav

Main is not the element of array, its a part of a element of array

Main 不是数组的元素,它是数组元素的一部分

function array_cleanup( $array, $todelete )
{
    $cloneArray = $array;
    foreach( $cloneArray as $key => $value )
    {
        if (strpos($value, $todelete ) !== false)   
           unset( $array[ $key ] );     //$array[$key] = str_replace($toDelete, $replaceWith, $value) ;  // add one more argument $replaceWith to function 

    }
    return $array;
}

Edit:

编辑:

with array

带阵列

function array_cleanup( $array, $todelete )
    {
      foreach($todelete as $del){
        $cloneArray = $array;
        foreach( $cloneArray as $key => $value )
        {          
            if (strpos($value, $del ) !== false)   
               unset( $array[ $key ] );     //$array[$key] = str_replace($toDelete, $replaceWith, $value) ;  // add one more argument $replaceWith to function 

           }
    }
     return $array;
}

  $newarray = array_cleanup( $cats, array('Category:Main') );

回答by stevenwoodson

Wanted to note that while the accepted answer works there's a built in PHP function that handles this called array_filter. For this particular example it'd be something like:

需要注意的是,虽然接受的答案有效,但有一个内置的 PHP 函数可以处理这个称为array_filter 的函数。对于这个特定的例子,它会是这样的:

public function arrayRemoveMain($var){
    if ( strpos( $var, "Category:Main" ) !== false ) { return false; }
    return true;
}

$newarray = array_filter( $cats, "arrayRemoveMain" );

Obviously there are many ways to do this, and the accepted answer may very well be the most ideal situation especially if there are a large number of $todelete options. With the built in array_filter I'm not aware of a way to pass additional parameters like $todelete so a new function would have to be created for each option.

显然有很多方法可以做到这一点,接受的答案很可能是最理想的情况,尤其是在有大量 $todelete 选项的情况下。使用内置的 array_filter,我不知道有什么方法可以传递像 $todelete 这样的附加参数,因此必须为每个选项创建一个新函数。

回答by Felipe Neuhauss

It's easy.

这很简单。

$times = ['08:00' => '08:00','09:00' => '09:00','10:00' => '10:00'];
$timesReserved = ['08:00'];
$times = (function() use ($times, $timesReserved) {
        foreach($timesReserved as $time){
            unset($times[$time]);
        }
        return $times;
    })();

回答by Robert Sinclair

The question is "how do I remove specific keys".. So here's an answer with array filter if you're looking to eliminate keys that contain a specific string, in our example if we want to eliminate anything that contains "alt_"

问题是“我如何删除特定键”..如果您想删除包含特定字符串的键,那么这里有一个数组过滤器的答案,在我们的示例中,如果我们想删除包含“alt_”的任何内容

$arr = array(
"alt_1"=>"val", 
"alt_2" => "val", 
"good key" => "val"
);

function remove_alt($k) {
    if(strpos($k, "alt_") !== false) {
        # return false if there's an "alt_" (won't pass array_filter)
        return false;
    } else {
        # all good, return true (let it pass array_filter)
        return true;
    }

}

$new = array_filter($arr,"remove_alt",ARRAY_FILTER_USE_KEY);
                                        # ^ this tells the script to enable the usage of array keys!

This will return

这将返回

array(""good key" => "val");

array(""好键" => "val");