untuark typeError:无法访问字符串类型字符串的偏移
在更新为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题在于,在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 whatget_post_meta()
is returning for you.Hope this helps!