在WordPress中使用多维数组中的值删除键

时间:2014-02-22 07:04:42

标签: php arrays wordpress multidimensional-array

我需要能够从插入到WordPress选项表中的数组中删除自定义键,这是我使用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
   )
)

我尝试了不同的方法并且已经接近但无法实际删除/取消设置整个自定义键本身。假设我想取消设置“customkeytwo”,我需要一种方法来传递此自定义ID并将其删除:

处理程序示例:

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

这就是我删除它后的结果:

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

1 个答案:

答案 0 :(得分:2)

为什么不使用unset

unset($myoptions[$_REQUEST['id']]); // will remove "customkeytwo"
print_r($myoptions);