以下代码导致错误:Notice: Uninitialized string offset: 0
:
$defArray = array( "width" => "", "height" => "" );
$option = get_option( "myoption", $defArray );
//This throws the error
echo $option[ "width" ];
//This shows it as type "string"
var_dump( $option );
根据文档,我应该能够传递一个默认值,如果该选项中没有保存任何内容,就会返回该值(而且没有保存任何东西,我只是编造了它)。http://codex.wordpress.org/Function_Reference/get_option
为什么不使用默认值?
不知怎么的,在刷新页面时,创建了选项。我不确定是怎么提交的,因为我从未提交过表单。调用delete_option(...)
阻止了错误的发生。
您可以通过解析args来强制返回;
$defArray = array( "width" => "", "height" => "" );
$option = get_option( "myoption", $defArray );
$option = wp_parse_args( $option , $defArray );