【问题标题】:Remove stdclass object from array second time always fails第二次从数组中删除 stdclass 对象总是失败
【发布时间】:2016-02-04 19:06:47
【问题描述】:

您能帮我找出我的代码中的问题吗?我有一个从我的 json 字符串中删除图像的功能。我将为我的代码提供解释。

这是我的测试控制器,用于测试我的代码

<?php class Test extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('Model_products');
    }

    public function index(){
        echo 'This is test controller';

        $id_product = 1;
        $id_image = 3;
        $prod = $this->Model_products->getCurrentUserProduct($id_product);
        $images = json_decode($prod[0]->images);
        var_dump($images);
        foreach($images as $i){
            if($i->id_image == $id_image){
                unset($images[$i->id_image]);
            }
        }
        $n = 0;
        foreach($images as $i){
            $i->id_image = $n;
            $i->position = $n;
            $n++;
        }
        var_dump($images);
        $this->Model_products->updateProductImages($id_product, json_encode($images));
        die();
    }
}

第一个 var_dump($images) 输出是

array (size=5)
  0 => 
    object(stdClass)[24]
      public 'id_image' => int 0
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=102)
      public 'thumbName' => string 'f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/2b6cfd74e622c4a61793259438b90b39.jpg' (length=108)
      public 'imageName' => string '2b6cfd74e622c4a61793259438b90b39.jpg' (length=36)
      public 'position' => int 0
  1 => 
    object(stdClass)[30]
      public 'id_image' => int 1
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/458270b1c351bf23a4c943ae9b393eb5.jpg' (length=102)
      public 'thumbName' => string '458270b1c351bf23a4c943ae9b393eb5.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/425a2346219aca49e0c1048da14d6276.jpg' (length=108)
      public 'imageName' => string '425a2346219aca49e0c1048da14d6276.jpg' (length=36)
      public 'position' => int 1
  2 => 
    object(stdClass)[31]
      public 'id_image' => int 2
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=102)
      public 'thumbName' => string '11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/a0d9059a611d49ea84c1e57c683b643b.jpg' (length=108)
      public 'imageName' => string 'a0d9059a611d49ea84c1e57c683b643b.jpg' (length=36)
      public 'position' => int 2
  3 => 
    object(stdClass)[32]
      public 'id_image' => int 3
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/0702f21fc6f7773aa5f44f4115241a35.jpg' (length=102)
      public 'thumbName' => string '0702f21fc6f7773aa5f44f4115241a35.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/d9dfb48ddee4a20e35cb433619f0da0b.jpg' (length=108)
      public 'imageName' => string 'd9dfb48ddee4a20e35cb433619f0da0b.jpg' (length=36)
      public 'position' => int 3
  4 => 
    object(stdClass)[33]
      public 'id_image' => int 4
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/32e902b9e64758f4dca64039833cbf0a.jpg' (length=102)
      public 'thumbName' => string '32e902b9e64758f4dca64039833cbf0a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/e31eaac5e991baf862d2ecc264145642.jpg' (length=108)
      public 'imageName' => string 'e31eaac5e991baf862d2ecc264145642.jpg' (length=36)
      public 'position' => int 4

这是正确的,因为我的产品有 5 张图片

现在我遍历 $images 并且我不想删除数组对象,其中 $id_image = 3。 所以这是我认为正确的第二个 var_dump($images) 输出,因为在我的代码中我删除了一个数组并“重置”图像和位置的 id

array (size=4)
  0 => 
    object(stdClass)[24]
      public 'id_image' => int 0
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=102)
      public 'thumbName' => string 'f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/2b6cfd74e622c4a61793259438b90b39.jpg' (length=108)
      public 'imageName' => string '2b6cfd74e622c4a61793259438b90b39.jpg' (length=36)
      public 'position' => int 0
  1 => 
    object(stdClass)[30]
      public 'id_image' => int 1
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/458270b1c351bf23a4c943ae9b393eb5.jpg' (length=102)
      public 'thumbName' => string '458270b1c351bf23a4c943ae9b393eb5.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/425a2346219aca49e0c1048da14d6276.jpg' (length=108)
      public 'imageName' => string '425a2346219aca49e0c1048da14d6276.jpg' (length=36)
      public 'position' => int 1
  2 => 
    object(stdClass)[31]
      public 'id_image' => int 2
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=102)
      public 'thumbName' => string '11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/a0d9059a611d49ea84c1e57c683b643b.jpg' (length=108)
      public 'imageName' => string 'a0d9059a611d49ea84c1e57c683b643b.jpg' (length=36)
      public 'position' => int 2
  4 => 
    object(stdClass)[33]
      public 'id_image' => int 3
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/32e902b9e64758f4dca64039833cbf0a.jpg' (length=102)
      public 'thumbName' => string '32e902b9e64758f4dca64039833cbf0a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/e31eaac5e991baf862d2ecc264145642.jpg' (length=108)
      public 'imageName' => string 'e31eaac5e991baf862d2ecc264145642.jpg' (length=36)
      public 'position' => int 3 

所以问题现在发生了,如果我再次尝试返回此代码(只需按 F5) 我从第一个 var_dump($images) 得到输出

object(stdClass)[24]
  public '0' => 
    object(stdClass)[30]
      public 'id_image' => int 0
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=102)
      public 'thumbName' => string 'f81e86620f557fa9fc5e71d063c12d6a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/2b6cfd74e622c4a61793259438b90b39.jpg' (length=108)
      public 'imageName' => string '2b6cfd74e622c4a61793259438b90b39.jpg' (length=36)
      public 'position' => int 0
  public '1' => 
    object(stdClass)[31]
      public 'id_image' => int 1
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/458270b1c351bf23a4c943ae9b393eb5.jpg' (length=102)
      public 'thumbName' => string '458270b1c351bf23a4c943ae9b393eb5.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/425a2346219aca49e0c1048da14d6276.jpg' (length=108)
      public 'imageName' => string '425a2346219aca49e0c1048da14d6276.jpg' (length=36)
      public 'position' => int 1
  public '2' => 
    object(stdClass)[32]
      public 'id_image' => int 2
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=102)
      public 'thumbName' => string '11bbe81edace48a1d6e03159c72bfc7f.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/a0d9059a611d49ea84c1e57c683b643b.jpg' (length=108)
      public 'imageName' => string 'a0d9059a611d49ea84c1e57c683b643b.jpg' (length=36)
      public 'position' => int 2
  public '4' => 
    object(stdClass)[33]
      public 'id_image' => int 3
      public 'thumbImage' => string '/home/valor/development/euro-swap.com/public_html/images/products/32e902b9e64758f4dca64039833cbf0a.jpg' (length=102)
      public 'thumbName' => string '32e902b9e64758f4dca64039833cbf0a.jpg' (length=36)
      public 'image' => string '/home/valor/development/euro-swap.com/public_html/images/products/large/e31eaac5e991baf862d2ecc264145642.jpg' (length=108)
      public 'imageName' => string 'e31eaac5e991baf862d2ecc264145642.jpg' (length=36)
      public 'position' => int 3

还有以下错误

Fatal error: Cannot use object of type stdClass as array in /home/valor/development/euro-swap.com/public_html/server/application/controllers/Test.php on line 18

发生了什么?为什么代码第一次总是有效,但每次都失败? 如果您需要任何其他信息,请告诉我,我会提供。

【问题讨论】:

  • 你第二次没有数组,而是一个对象object(stdClass)[24]你的模型中一定发生了一些奇怪的事情
  • 漂亮的眼睛!可能我以错误的方式存储图像。我正在进一步研究
  • 你没有取消设置unset($images[$i-&gt;id_image]);中的一个元素吗?

标签: php arrays codeigniter stdclass


【解决方案1】:

你可以试试

json_decode($yourJson,true);

用于强制返回关联数组

【讨论】:

  • 干得好!这为我节省了很多工作,因为我正在重写整个函数。
【解决方案2】:

不能将对象( $object->property )用作 $object['property']

但这是对象,语法是 $obj->property

而数组是$array['offset']

或者使用 $query->result_array() 代替 $query->result()

并返回一个模型方法查询对象,这样更方便

【讨论】:

  • stdClass 由 json_decode 函数返回...因为我点击了 1
  • 是的,模型中已经写好了$query->result_array(),但是json_decode函数会自动返回stdClass对象,所以我需要使用son_decode($yourJson,true);
猜你喜欢
  • 2022-01-20
  • 2016-02-04
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多