2016-02-04 91 views
0

您能否幫我在代碼中找到問題?我有一個從我的json字符串中刪除圖像的功能。我會提供我的代碼解釋。從數組中刪除stdclass對象第二次總是失敗

這是我的測試我的代碼

<?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($圖像)輸出

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張圖片

現在測試控制器我循環$圖像,我不想刪除數組對象,其中$ id_image = 3. 所以這是第二個var_dump($ images)輸出,這在我看來是正確的,因爲在我的代碼刪除一個陣列和「復位」的圖像標識和定位

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輸出($圖像)

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 

這是怎麼回事?爲什麼代碼總是第一次工作,但下一次總是失敗? 如果您需要任何其他信息,請讓我知道,我會提供。

+1

你沒有一個數組的第二次,但一個對象'對象(stdClass的)[24]'奇怪的事情在你的模型必須發生 – Gwendal

+0

漂亮的眼睛!可能我存儲的圖像是錯誤的方式。我正在研究 –

+0

難道你沒有取消設置'unset($ images [$ i-> id_image])中的一個元素;'? – Tpojka

回答

1

你可以試試

json_decode($yourJson,true); 

的力返回關聯數組

+0

幹得好!這節省了我很多工作,因爲我正在重寫整個功能。 –

+0

@熱情歡迎!對不起,我的英語不好。 – Naumov

-1

不能使用對象($對象 - >性)爲$對象[ '屬性']

但是,這是對象和語法是obj- $>屬性

AND陣列$陣列['偏移「]

或者使用查詢 - $> result_array(),而不是查詢 - $>結果()

而在一個模型的方法查詢對象,這更方便的返回

+0

stdClass通過json_decode函數返回...因爲我在模型中點擊了1 – Naumov

+0

yes已經寫了$ query-> result_array(),但是json_decode函數自動返回stdClass對象,所以我需要使用son_decode($ yourJson,true ); –