untuark typeError:无法访问字符串类型字符串的偏移

发布于 2025-02-11 14:55:32 字数 1102 浏览 2 评论 0原文

在更新为PHP 8后,我从标题中收到了错误。代码类似于下面的错误,它说该错误已在线: if(is_array($ course_data ['step'] ['h'] ['sfwd-lessons'])){

此处:

// bail out if this is not an event item
    if ('sfwd-courses' !== get_post_type($post_id)) {
        return;
    }
    $course_data  = get_post_meta($post_id, 'ld_course_steps', true);
    if (!empty($course_data)) {
        $lessons = array_keys($course_data['steps']['h']["sfwd-lessons"]);
        $quizes = array_keys($course_data['steps']['h']["sfwd-quiz"]);
    }

$lesson_data = [];
    // Extract all steps of the courses
    if(is_array($course_data['steps']['h']["sfwd-lessons"])){
        foreach($course_data['steps']['h']["sfwd-lessons"] as $lesson){
            $lesson_data = array_merge($lesson_data,array_values(array_keys($lesson["sfwd-topic"])));
            $lesson_data = array_merge($lesson_data,array_values(array_keys($lesson["sfwd-quiz"])));
            foreach($lesson["sfwd-topic"] as $topic){
                $lesson_data = array_merge($lesson_data,array_values(array_keys($topic["sfwd-quiz"])));
            }
        }
    }

I got the error from the title after I update to PHP 8. The code is similar to the below and it said the error is on the line:
if(is_array($course_data['steps']['h']["sfwd-lessons"])){

full code here:

// bail out if this is not an event item
    if ('sfwd-courses' !== get_post_type($post_id)) {
        return;
    }
    $course_data  = get_post_meta($post_id, 'ld_course_steps', true);
    if (!empty($course_data)) {
        $lessons = array_keys($course_data['steps']['h']["sfwd-lessons"]);
        $quizes = array_keys($course_data['steps']['h']["sfwd-quiz"]);
    }

$lesson_data = [];
    // Extract all steps of the courses
    if(is_array($course_data['steps']['h']["sfwd-lessons"])){
        foreach($course_data['steps']['h']["sfwd-lessons"] as $lesson){
            $lesson_data = array_merge($lesson_data,array_values(array_keys($lesson["sfwd-topic"])));
            $lesson_data = array_merge($lesson_data,array_values(array_keys($lesson["sfwd-quiz"])));
            foreach($lesson["sfwd-topic"] as $topic){
                $lesson_data = array_merge($lesson_data,array_values(array_keys($topic["sfwd-quiz"])));
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-02-18 14:55:33

这里的问题在于,在PHP 8中,此代码错误用于发出“警告”,但现在是致命的错误。不用担心,这可能很容易解决!

问题在于,示例代码中的$ course_data作为字符串返回,并且您正在尝试在其上使用阵列偏移。尝试var_dump($ course_data)立即定义$ cousecom_data并查看其返回的数据类型。

请注意,get_post_meta()被设置为true中的第三个参数使其返回“单个值”,而不是数组,因此可能是问题。很难确切地说明这里的修复是什么而不看到您的数据是什么样子,但是第一步肯定是检查get_post_meta()正在为您返回的内容。

希望这有帮助!

The problem here is that in PHP 8, this code error used to throw a 'warning' but now it's a fatal error. Don't worry, it's probably a pretty easy fix!

The issue is that $course_data from your sample code is being returned as a string, and you're trying to use array offsets to on it. Try var_dump($course_data) right after the line that defines $course_data and see what type of data it's returning.

Note that the third argument in get_post_meta() being set to true makes it return a 'single value', vs an array, so that's likely the issue. It's tough to say exactly what the fix is here without seeing what your data looks like, but the first step is definitely examining what get_post_meta() is returning for you.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文