9

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

0

4 Answers 4

10

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') );
Sign up to request clarification or add additional context in comments.

7 Comments

should be strpos($value, $toDelete) !== false
I also get parser error on if (strpos($value, $toDelete )! == false)
You don't replace it at all. It's a function argument. You would call it with 'Category:Main', or whatever string you want filtered.
@user585303 : you can use str_replace(). php.net/manual/en/function.str-replace.php
should be ` !== false, not ! == false`
|
3

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.

1 Comment

I'm with you! Functional programming should be more popular. Could even put the function right inside the brackets! Then, to add support for $todelete, you would create a closure with "use ($todelete)" right after the function parameters: $todelete="Category:Main"; $newarray = array_filter($cats, function($cat) use($todelete){ if...});
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.