0

I need to be able to remove a custom key from an array inserted into a WordPress options table, here is the array i get in return using print_r:

Array (
[customkeyone] => Array (
   [itemname] => 'name'
   [sortorder] => 1
   [date] => 1393042529
   [target] => 1
   )
[customkeytwo] => Array (
   [itemname] => 'nametwo'
   [sortorder] => 1
   [date] => 1393042525
   [target] => 1
   )
[customkeythree] => Array (
   [itemname] => 'namethree'
   [sortorder] => 1
   [date] => 1393042522
   [target] => 1
   )
)

I tried different methods and have come close but not able to actually remove/unset the entire custom key itself. Lets say I want to unset "customkeytwo", I would need a way to pass this custom id and remove it:

Handler example:

if (isset($_GET['delete'])) {

    //ID in this case is "customkeytwo" 
    if ($_REQUEST['id'] != ''){

                $id = $_REQUEST['id'];
                $myoptions = get_option('myoptions'); 

                //some method to unset the custom key with $id

                //update with new results
                update_option('myoptions', $myoptions); 

And this is how I want the result to be after I remove it:

Array (
[customkeyone] => Array (
   [itemname] => 'name'
   [sortorder] => 1
   [date] => 1393042529
   [target] => 1
   )
[customkeythree] => Array (
   [itemname] => 'namethree'
   [sortorder] => 1
   [date] => 1393042522
   [target] => 1
   ) 
)

1 Answer 1

2

Why not just use unset ?

unset($myoptions[$_REQUEST['id']]); // will remove "customkeytwo"
print_r($myoptions);
Sign up to request clarification or add additional context in comments.

5 Comments

have you debug it ? What gives you var_dump($options) & var_dump($_REQUEST['id']) ?
var_dump($_REQUEST['id']) ----> string(12) "customkeytwo"
i'm able to remove key values, but not the key itself
Sorry, this is actually correct, after having some good rest I was able to see the small mistake on my side with the update option line, but it is now working and this method is correct
Can you share your "small mistake" and what you did to fix it? I have the same issue and I have tried to unset the array and then resubmit it with update_option but I get an empty entry for the option when I do that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.