plugins - Remove a custom options namevalue pair

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!

UPDATE -based on MMK's help below, here's the code:

$toremove = 605 // I actually use a var like so $toremove = $_REQUEST['somevar']; I'm just using 605 as an example
$selectid = get_option('swcs_postpage_id');
$key = array_search ($toremove, $selectid);
unset($selectid[$key]);
update_option(swcs_postpage_id, $selectid);

I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is complicated by the fact that the custom option is an array, not just a variable/value. The data is stored in an options field with the name:

swcs_postpage_id

My test data currently contains two items in the array:

a:2:{i:0;s:2:"10";i:1;s:3:"605";}

These were added by the user selecting a number of options in a form in the main plugin file. In my test data the two values are '10' and '605'.

I am not sure how to go about removing an item from the array. E.g. if the user selects '605' to delete, how do I remove that name/value from the array, leaving all the others?

I have read up on delete_option and update_option but neither are making an awful lot of sense to me at the moment!

UPDATE -based on MMK's help below, here's the code:

$toremove = 605 // I actually use a var like so $toremove = $_REQUEST['somevar']; I'm just using 605 as an example
$selectid = get_option('swcs_postpage_id');
$key = array_search ($toremove, $selectid);
unset($selectid[$key]);
update_option(swcs_postpage_id, $selectid);
Share Improve this question edited Oct 12, 2015 at 14:17 Kev asked Oct 12, 2015 at 11:40 KevKev 572 silver badges12 bronze badges 3
  • Your option swcs_postpage_id contains an array. If your user selects 605 then simply do a search like so and unset your value unset( key( $your_var_containing_deletion_key ) - 1 ); More info on key: php/manual/en/function.key.php" and on unset: php/manual/en/function.unset.php – MMK Commented Oct 12, 2015 at 12:41
  • Thanks MMK (again) - I'm just bout to edited my post to reflect your answer. – Kev Commented Oct 12, 2015 at 14:09
  • You can post an answer to your own question which is more appropriate than adding it to the question. – kraftner Commented Oct 12, 2015 at 14:19
Add a comment  | 

1 Answer 1

Reset to default 0

based on MMK's help below, here's the code:

$toremove = 605 // I actually use a var like so $toremove = $_REQUEST['somevar']; I'm just using 605 as an example
$selectid = get_option('swcs_postpage_id');
$key = array_search ($toremove, $selectid);
unset($selectid[$key]);
update_option(swcs_postpage_id, $selectid);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744899558a4599897.html

相关推荐

  • plugins - Remove a custom options namevalue pair

    I'm writing a plugin of which one aspect is the deletion of a custom option in the Options table. The situation is

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信