php - 如何修复这个非法偏移类型错误 [英] php - How do I fix this illegal offset type error

查看:30
本文介绍了php - 如何修复这个非法偏移类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要了

非法偏移类型

此代码的每次迭代都会出错.这是代码:

error for every iteration of this code. Here's the code :

$s = array();
for($i = 0; $i < 20; $i++){
    $source = $xml->entry[$i]->source;
    $s[$source] += 1;    
}

print_r($s)

推荐答案

Illegal offset type 当您尝试使用对象array 作为索引键.

Illegal offset type errors occur when you attempt to access an array index using an object or an array as the index key.

示例:

$x = new stdClass();
$arr = array();
echo $arr[$x];
//illegal offset type

您的 $xml 数组在 $xml->entry[$i]->source 处包含一个对象或数组,用于 $i 的某个值,并且当您尝试将其用作 $s 的索引键时,您会收到该警告.您必须确保 $xml 包含您想要的内容并且您正在正确访问它.

Your $xml array contains an object or array at $xml->entry[$i]->source for some value of $i, and when you try to use that as an index key for $s, you get that warning. You'll have to make sure $xml contains what you want it to and that you're accessing it correctly.

这篇关于php - 如何修复这个非法偏移类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆