开发者

How do I remove specific keys from Array? PHP

开发者 https://www.devze.com 2023-02-14 19:11 出处:网络
I am trying to remove keys from array. Here is what i get from printing print_r($cats); Array ( [0] => <a href=\"/website/index.php/Category:All\" title=\"Category:All\">All</a> &

I am trying to remove keys from array.

Here is what i get from printing 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="Ca开发者_如何学Pythontegory: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


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

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') );


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:

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.


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;
    })();


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_"

$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");

0

精彩评论

暂无评论...
验证码 换一张
取 消