How do I correct this Illegal String Offset?

2019-07-21 09:33发布

我收到此错误“警告:非法串偏移‘类型’中/home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php在线32”

我意识到的代码的文件在这一节是错误的,但我不是PHP这个伟大但我想知道,如果有人可以帮助我重新写本节,以消除误差。 谢谢! (其上是if语句的开头下面线32启动错误)

下面是代码:

/* new version */
    function get_attachment_struct( $inputs ){
        $attach = array();

    if( $inputs['type'] == 'attach' ){ 
            $name = $inputs['name'];
            $attach = array(
                0 => array(
                    'name' => $name,
                    'type' => 'text',
                    'label' =>  'Attachment URL',
                    'lvisible' => false,
                    'upload' => true,
                ),
                1 => array(
                    'name' => $name .'_id',
                    'type' => 'hidden',
                    'upload' => true
                ),
            );

            if( isset( $inputs[ 'classes' ] ) ){
                $attach[0]['classes'] = $inputs[ 'classes' ];
                $attach[1]['classes'] = $inputs[ 'classes' ] . '_id';
            }
        }
        return $attach;
    }

    /* new version */

Answer 1:

if ($inputs['type'] == 'attach') {

该代码是有效的,但它期望功能参数$inputs是一个数组。 当使用“非法串偏移”警告$inputs['type']意味着该函数被传递字符串而不是阵列。 (然后由于串偏移是一个数字, 'type'是不适合的。)

所以在理论上,问题不在于此,用的代码没有提供正确的参数调用者。

但是,此警告消息是新的PHP 5.4。 老版本没有警告,如果发生这种情况。 他们会默默地转换'type'0 ,然后尝试得到字符串的字符0(第一个字符)。 因此,如果这个代码是这样运作的,这是因为滥用字符串这样并未对PHP 5.3及以下的任何投诉。 (很多老的PHP代码升级后遇到此问题。)

您可能要调试为什么被通过检查调用代码给出一个字符串的函数,并找出看重它具有通过执行var_dump($inputs); 在函数。 但是,如果你只是想关闭该警告直至使其行为像PHP 5.3,行更改为:

if (is_array($inputs) && $inputs['type'] == 'attach') {


Answer 2:

if(isset($rule["type"]) && ($rule["type"] == "radio") || ($rule["type"] == "checkbox") )
{
    if(!isset($data[$field]))
        $data[$field]="";
}


Answer 3:

我得到同样的错误在WP当我使用PHP版本7.1.6 - 只要把你的PHP版本,返回到7.0.20和错误将消失。



文章来源: How do I correct this Illegal String Offset?
标签: php wordpress