为什么我的 PHP 数组给我“不能使用 stdClass 类型的对象作为数组”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18535885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:45:04  来源:igfitidea点击:

Why does my PHP array give me "Cannot use object of type stdClass as array"?

phparrays

提问by M dunbavan

Can anyone can help here, I am using an array to get some data split up and on the view this is attached to I get this error above.

任何人都可以在这里提供帮助,我正在使用一个数组来拆分一些数据,并且在附加到的视图中我得到了上面的这个错误。

here is my code:

这是我的代码:

PHP for ajax view:

PHP 用于 ajax 视图:

$images = array();
  $data_link = array();
  $data_id = array();
  $data_likes = array();
  $data_text = array();
  foreach ($media as $data) {
    //dd($data->caption);
    $images[] = array(
        //dd($data->caption),
        "data_url"=>$data->images->standard_resolution->url,
        "data_link"=>$data->link,
        "data_text" => $data->caption['text'],
        "data_id"=>$data->getId(),
        "data_likes"=>$data->likes->count,
        "data_like"=>$current_user->likes($data),
        "data_token"=>$token

    );
  }
  echo json_encode(array(
    'next_id' => $media->getNextMaxTagId(),
    'images'  => $images
  ));

I then access the data with a load more button. When I debug the code for $data->captionI get this:

然后我使用加载更多按钮访问数据。当我调试代码时,$data->caption我得到了这个:

object(stdClass)#62 (4) {
  ["created_time"]=>
  string(10) "1377775401"
  ["text"]=>
  string(48) "New arrival #folkclothing reversible knit/Hymanet"
  ["from"]=>
  object(stdClass)#63 (4) {
    ["username"]=>
    string(18) "vanmildertclothing"
    ["profile_picture"]=>
    string(76) "http://images.ak.instagram.com/profiles/profile_24567722_75sq_1342623790.jpg"
    ["id"]=>
    string(8) "24567722"
    ["full_name"]=>
    string(11) "Van Mildert"
  }
  ["id"]=>
  string(18) "533141169038331174"
}

Does anyone know why I get this error?

有谁知道我为什么会收到这个错误?



Edited below here as the error is not as expected

由于错误不符合预期,在此处进行了编辑



The ajax jquery call for this is as below:

ajax jquery 调用如下:

  $('#more').click(function() {
    var tag   = $(this).data('tag'),
        maxid = $(this).data('maxid');

    $.ajax({
      type: 'GET',
      url: '/ajax',
      data: {
        tag: tag,
        max_tag_id: maxid
      },
      dataType: 'json',
      cache: false,
      complete: function(){
      },
      success: function(data) {
        // Output data
        $.each(data.images, function(i, src) {
        var $content = $('<article class="instagram-image"><form id="'+ data.images[i].data_token +'" class="forms status-'+ data.images[i].data_like +'" action="'+base+'" method="post"><a class="fancybox" rel="folk-1" href="' + data.images[i].data_url + '"><img alt="' + data.images[i].data_text + '" src="' + data.images[i].data_url + '" alt="' + data.images[i].data_text + '" /></a><div class="formSubmit-feedback"></div><input type="hidden" name="id" value="'+ data.images[i].data_id +'"><p>'+ data.images[i].data_likes +'</p></form></article>');
            var duration = 1000, n = 0.1;
          $content.appendTo('section#images');
          $item = $content.find('form');
          if( $item.hasClass("status-false") ){
                $item.addClass("notLiked");
                //$('.notLiked').find('button.unlike').hide();
                $item.find('a').after('<button class="ajax instabtn button-like like icon-heart" type="submit" name="action" value="Like"></button>');
            }
            if( $item.hasClass("status-true") ){
                $item.addClass("Liked");
                //$('.Liked').find('button.like').hide();
                $item.find('a').after('<button class="ajax instabtn button-unlike unlike icon-heart" type="submit" name="action" value="Unlike"></button>');
            }
          });
        // Store new maxid
        $('#more').data('maxid', data.next_id);
      },
      error:function(){
        $('#error-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
    }
    });
  });

On the page I have a button that loads more instagram images into the container, it works okay with everything else but will not work with the caption->text which I am trying to access. If I go to the url /ajax I get the data I want and it shows the text in the text_data array but when it comes to loading that into the images from the load more I get that error.

在页面上,我有一个按钮可以将更多 instagram 图像加载到容器中,它可以与其他所有内容一起使用,但不能与我尝试访问的标题 - >文本一起使用。如果我转到 url /ajax,我会得到我想要的数据,它会在 text_data 数组中显示文本,但是当涉及到从 load more 将其加载到图像中时,我会收到该错误。

This is the exact error I get:

这是我得到的确切错误:

Error rendering view: [instagram.ajax]Trying to get property of non-object

Error rendering view: [instagram.ajax]Trying to get property of non-object

Not as simple as I first thought.

不像我最初想的那么简单。

回答by deceze

Exactly as it says: $data->captionis an object, but you're trying to access it like an array using $data->caption['text']. Access it like an object instead:

正如它所说:$data->captionis an object,但您试图像使用数组一样访问它$data->caption['text']。像对象一样访问它:

$data->caption->text

回答by webbiedave

$data->captionis an object but your code is trying to access it as if it were an array.

$data->caption是一个对象,但您的代码正试图访问它,就好像它是一个数组。

Change $data->caption['text']to $data->caption->text

更改$data->caption['text']$data->caption->text

回答by Bart Friederichs

$data->captionis an object, and you are accessing it as an array.

$data->caption是一个对象,您将它作为数组访问。

Use $data->caption->textinstead.

使用$data->caption->text来代替。

回答by M dunbavan

Right the answer was this:

正确的答案是这样的:

foreach ($media as $data) {
    $title = (isset($data->caption))?mb_substr($data->caption->text,0,70,"utf8"):null;
    $images[] = array(
        "data_url"=>$data->images->standard_resolution->url,
        "data_link"=>$data->link,
        "data_text" => htmlspecialchars($title),
        "data_id"=>$data->getId(),
        "data_likes"=>$data->likes->count,
        "data_like"=>$current_user->likes($data),
        "data_token"=>$token

    );
  }

So I declared the variable before the images array and then accessed it in the array.

所以我在图像数组之前声明了变量,然后在数组中访问它。