2012-08-17 53 views
0

例如說我有一個名爲$列表的對象數組是這樣的:在php中刪除對象數組中的對象?

<(stdClass)#16 (3) { 
    ["1"]=> 
    <(8) "50504496" 
    ["2"]=> 
    <(8) "12435374" 
    ["3"]=> 
    <(8) "12436374" 

Im做對象數組上一個foreach,如果他們存在於數據庫即

foreach($list as $l){ 

    //do the query 

    if(it exists){ 
     //remove from objects: this is where i need help!! 
     } 
    } 

刪除他們,我有數據庫邏輯,即時通訊只是堅持知道我可以如何刪除對象,我想也許我應該創建一個新的對象,並添加他們。感謝 }

+2

'unset' [php.net/unset](http://li.php.net/unset) – EaterOfCode 2012-08-17 11:59:52

回答

4

用途:未設置

foreach($list as $key => $obj){ 
    if(exists ..) { 
     unset($list[$key]); 
    } 
} 
+0

值謝謝brillant答案,什麼設計......代表1,2或3 $ var名稱 – user1551482 2012-08-17 12:06:24

0

運行查詢,如果返回>0的行數,那麼你有一個結果,使用unset($object->$var)取消設置變量

0

嘗試,如:

foreach($list as $li) 
    { 
     if($li) 
     { 
      //copy into another array. 
     } 
    } 

i thnk它可能很簡單

+0

如何複製到另一個數組? – user1551482 2012-08-17 12:06:48

0

試試like: $ i = 0;

foreach($list as $li) 
    { 
     if($li) 
     { 
      $b[i] = $li; 
      $i++; 
     } 
    } 

陣列 '$ B' 將持有$列表,它不是空