【问题标题】:Cannot use object of type stdClass as array in不能使用 stdClass 类型的对象作为数组
【发布时间】:2012-01-19 03:30:14
【问题描述】:

我有时会出现以下错误 致命错误:不能使用 stdClass 类型的对象作为数组 in.. 使用此功能:

function deliciousCount($domain_name)
{
    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
        )
    );
    if ($data) {
        return $data[0]->total_posts;
    } else {
        return 0;
    }
}

$delic = deliciousCount($domain_name);

但此错误有时仅针对特定域发生 有什么帮助吗?

【问题讨论】:

  • 您能告诉我们一个“触发”错误的 url 示例吗?必须是来自delicious.com 的响应是有效的json 但不是[ ... ] 形式的东西。
  • 注意:我的假设是,not 触发错误的唯一时间是如果/当请求 file_get_contents 请求不成功.. 或其他在这种情况下,json_decode 将返回 false 或 null 如果没有要解码的内容;因此,他的函数将返回 false 从而避免错误。
  • 域是xcursionsindia.in 有时会发生有时不会发生
  • delicious.com的这个功能坏了。对于像google.de 这样的现有域,一次您将获得一个条目 ([{"url":...}]),而其他时候您将获得一个空对象 ({})。对于不存在的域,例如 example.example [] {}
  • 事实上,Saxoier 你有权利,那么解决方案是什么?

标签: php function stdclass


【解决方案1】:

json_decode 返回一个 stdClass 的实例,您不能像访问数组一样访问它。 json_decode 确实可以通过将 true 作为第二个参数传递来返回一个数组。

【讨论】:

    【解决方案2】:

    根据the manual,有一个可选的第二个boolean 参数指定是否应将返回的对象转换为关联数组(默认为false)。如果您想将其作为数组访问,则只需将 true 作为第二个参数传递。

    $data = json_decode(
        file_get_contents(
            "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
        ),
        true
    );
    

    【讨论】:

      【解决方案3】:
      function deliciousCount($domain_name)
      {
          $data = json_decode(
              file_get_contents(
                  "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
              )
          );
          if ($data) {
              return $data->total_posts;
          } else {
              return 0;
          }
      }
      
      $delic = deliciousCount($domain_name); 
      

      function deliciousCount($domain_name)
      {
          $data = json_decode(
              file_get_contents(
                  "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name",true
              )
          );
          if ($data) {
              return $data['total_posts'];
          } else {
              return 0;
          }
      }
      
      $delic = deliciousCount($domain_name);
      

      【讨论】:

        【解决方案4】:

        在使用 $data 作为数组之前:

        $data = (array) $data;
        

        然后简单地从数组中获取你的 total_posts 值。

        $data[0]['total_posts']
        

        【讨论】:

          【解决方案5】:
          function deliciousCount($domain_name) {
              $data = json_decode(
                  file_get_contents(
                      "http://feeds.delicious.com/v2/json/urlinfo/data?url=$domain_name"
                  )
              );
              // You should double check everything because this delicious function is broken
              if (is_array($data) && isset($data[ 0 ]) &&
                  $data[ 0 ] instanceof stdClass  && isset($data[ 0 ]->total_posts)) {
                  return $data[ 0 ]->total_posts;
              } else {
                  return 0;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-12-07
            • 2012-03-29
            • 2011-10-12
            相关资源
            最近更新 更多