arrays - 如何从多维数组更新 wordpress 选项

标签 arrays wordpress multidimensional-array

我正在为 wordrpess 开发一个主题,我想在其中为主题设置添加一个选项管理页面。无法让我的大脑处理这个过程。这是我的代码:

$option_settings = (array(
                array('Section1', array(
                    array(  'ID'=>'id_name1', 
                            'Label'=>'Title1', 
                            'Value'=>'The title1 bar',
                            'Desc'=>'Description Goes Here1',
                            'Type'=>'input_text',
                            'Button'=>'upload'
                    ),
                array('Section2', array(
                    array(  'ID'=>'id_name2', 
                            'Label'=>'Title2', 
                            'Value'=>'The title2 bar',
                            'Desc'=>'Description Goes Here2',
                            'Type'=>'input_text',
                            'Button'=>'upload'
                    ),
                ))
));

if (!get_option('my_option_settings')) {
add_option('my_option_settings',$option_settings);
}

$options = get_option('my_option_settings');

if ($_REQUEST['save_settings']) {

//this is where my brain snaps huhu 

}

echo '<form method="post" action="index.php" id="form_settings">';
echo '<p class="submit message"><input type="submit" value="Save Changes" name="save_settings" /></p>';
foreach ($options as $section) {
echo '<h3>'.$section[0].'</h3>';
foreach ($section[1] as $option => $value) {
    switch($value['Type']) {
        case "input_text":
            echo '<p><strong>'.$value[Label].'</strong> <input type="text" name="'.$value['ID'].'" id="'.$value['ID'].'" value="'.$value['Value'].'" /></p>';
            break;
    }

}
echo '</form>';

我主要关心的是如何在数组中编辑数组以及如何传递请求。非常感谢任何帮助提前致谢。

更新的问题:

好吧,假设 $option-settings 是来自 wordpress 的选项数据库的内容。原因是我想在 wordpress 数据库中只有 1 个选项,并通过数组存储它们以获得更多组织数据。

首先是获取值并将其分配给变量:

$fetchOption = get_option('my_option_settings');

现在我将编辑或更新 $fetchOption 变量中的数组。

foreach ($options as $section) {
    foreach ($section[1] as $option => $value) {
        $value['Value'] = [$_POST[$value['ID']]];

    }

}

最后是如何将更改后的值放回到 $fetchOption 变量中,并使用 update_options('my_option_settings', $fetchOption) 更新数据库。

这样合适不合适?最好的做法是什么?我可以将它们分配给 1 个选项,但我想它有点乱。再次感谢!

最佳答案

嗯。我真的希望您已经找到答案,但是..由于有人可能需要此信息,我想将其添加到此处。

首先,link到资源。

然后,我用作“来源”的答案:

As far WordPress is concerned - your multi-dimensional array is one option.

To update just part of the multi-dimensional array its necessary to retrieve the entire array, alter it accordingly and then update the entire array.

Suppose your multi-dimensional array is as follows:

my_options = array(
  'option_a'=>'value_a',
  'option_b'=>'value_b',
  'inner_array'=>array(
       'foo' => 'bar',
       'hello' => 'world',
   ),
  'option_c'=>'value_c'
)

And suppose you want to update the value of the 'hello' option from 'world' to 'moon'

//Get entire array
$my_options = get_option('my_options');

//Alter the options array appropriately
$my_options['inner_array']['hello'] = 'moon';

//Update entire array
update_option('my_options', $my_options);

希望这有助于 Stackoverflow 访问者在涉及 Wordpress 选项时管理多维数组:)

关于arrays - 如何从多维数组更新 wordpress 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9023745/

相关文章:

arrays - 将字节数组[十六进制]转换为字符数组或字符串类型+ Arduino

javascript - 将具有公共(public)键的对象分组到数组中

arrays - 如何在Powershell中对数组进行分组?

wordpress - 在 Woocommerce 中设置每个购物车商品的最大重量

Wordpress 所见即所得编辑器自动添加带有样式的跨度类

c++ - C++ 中的二维 int 数组

Java数组解析

php - Wordpress - 媒体查询不工作

php - 如何获取多维数组的所有路径

javascript - 在javascript中将字符串转换为二维数组