Php &引用;不能将字符串偏移量用作数组;致命错误

Php &引用;不能将字符串偏移量用作数组;致命错误,php,wordpress,Php,Wordpress,我已经看到了类似问题的答案,但它们没有帮助解决问题。非常感谢您的帮助。谢谢 问题线: $postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );

我已经看到了类似问题的答案,但它们没有帮助解决问题。非常感谢您的帮助。谢谢

问题线:

$postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
完整代码:

function cp_process_new_image() {
global $wpdb;
$postvals = '';

for ( $i=0; $i < count( $_FILES['image']['tmp_name'] ); $i++ ) {
    if ( !empty($_FILES['image']['tmp_name'][$i]) ) {
        // rename the image to a random number to prevent junk image names from coming in
        $renamed = mt_rand( 1000,1000000 ).".".mastheme_find_ext( $_FILES['image']['name'][$i] );

        //Because WP can't handle multiple uploads as of 2.8.5
        $upload = array( 'name' => $renamed,'type' => $_FILES['image']['type'][$i],'tmp_name' => $_FILES['image']['tmp_name'][$i],'error' => $_FILES['image']['error'][$i],'size' => $_FILES['image']['size'][$i] );

        // need to set this in order to send to WP media
        $overrides = array( 'test_form' => false );

        // check and make sure the image has a valid extension and then upload it
        $file = cp_image_upload( $upload );

        if ( $file ) // put all these keys into an array and session so we can associate the image to the post after generating the post id
            $postvals['attachment'][$i] = array( 'post_title' => $renamed,'post_content' => '','post_excerpt' => '','post_mime_type' => $file['type'],'guid' => $file['url'], 'file' => $file['file'] );
    }
}
return $postvals;
函数cp\u process\u new\u image(){
全球$wpdb;
$postvals='';
对于($i=0;$i$rename,'type'=>$\u文件['image']['type'][$i],'tmp\u name'=>$\u文件['image'][$i],'error'=>$\u文件['image']['error'][$i],'size'=>$\u文件['image'['size'][$i]);
//需要设置此选项才能发送到WP介质
$overrides=数组('test_form'=>false);
//检查并确保图像具有有效的扩展名,然后将其上载
$file=cp\u image\u upload($upload);
if($file)//将所有这些键放入一个数组和会话中,这样我们就可以在生成post id后将图像与post关联起来
$postwals['attachment'][$i]=数组('post\u title'=>$重命名,'post\u content'=>'','post\u摘录'=>'','post\u mime\u type'=>$file['type'],'guid'=>$file['url'],'file'=>$file['file']);
}
}
返回$postvals;
}

“无法将字符串偏移量用作数组”致命错误

如代码所示,
$postvals
是字符串而不是数组,因此附件的键不存在

更改:

$postvals = '';
至以下其中一项:

$postvals = array();
$postvals = [];

将变量初始化为正确的类型。

您能否演示如何初始化/使用
$postvals
当然,请参阅添加的代码。谢谢
postvals
不是数组。所以附件的索引不存在。是的,你是对的。谢谢,非常感谢!