0

Can you please help me find a problem in my code. I have a function for deleting images from my json string. I will provide my code with explanation.

This is my test controller for testing my code

<?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();
    }
}

First var_dump($images) output is

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

which is correct, since my product has 5 images

Now i loop through $images and i wan't to delete array object, where $id_image = 3. So this is second var_dump($images) output which is in my opinion correct, since in my code i remove one array and "reset" id of images and positions

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 

So problem happens now, if i try to return this code again (just pressing F5) I get the output from first 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

And the following error

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

What is happening? Why code always works for first time, but always fails each next time? If you need any additional information, please let me know and i will provide.

3
  • 1
    You don't have an array the second time, but an object object(stdClass)[24] Something weird must happen in your model Commented Feb 4, 2016 at 19:37
  • nice eyes! Probably i'm storing images wrong way. I'm researching further Commented Feb 4, 2016 at 19:41
  • Didn't you unset one element in unset($images[$i->id_image]);? Commented Feb 4, 2016 at 22:38

2 Answers 2

1

you can try

json_decode($yourJson,true);

for force return associative array

Sign up to request clarification or add additional context in comments.

1 Comment

Good job! That saved me a lot of work, because i was in middle of rewriting whole function.
0

Cannot use object ( $object->property ) as $object['property']

But this is object and syntax is $obj->property

And array is $array['offset']

Or use $query->result_array() instead $query->result()

And return in a model method query object, this more convenient

2 Comments

stdClass return by json_decode function... Because I click down 1
yes in model, there is already written $query->result_array(), but json_decode function automatically returns stdClass object, so i needed to use son_decode($yourJson,true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.