2013-03-30 50 views
-1

我正在處理一個多維數組,其中將包含可變數目的空對象。我寫了一個腳本來簡單地取消設置()空的,並且它工作正常。但是,即使沒有echo()函數,運行循環時也會打印非空值,而我似乎無法弄清楚原因。在foreach循環中使用無命令回顯數組項目

,將通過我的循環使用運行的陣列的一個例子:

Array 
(
[0] => Group Object 
    (
     [_title] => <strong>Abdomen</strong><ul> 
     [_issues] => <li><strong>Ascites</strong>: 1</li><li><strong>Blood Clot</strong>: 2</li> 
    ) 

[1] => Group Object 
    (
     [_title] => <strong>Air Sac</strong><ul> 
     [_issues] => <li><strong>Cloudy</strong>: 1</li> 
    ) 

[2] => Group Object 
    (
     [_title] => <strong>Beak Treatment</strong><ul> 
     [_issues] => <li><strong>Severe</strong>: 3</li> 
    ) 

[3] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[4] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[5] => Group Object 
    (
     [_title] => <strong>Crop</strong><ul> 
     [_issues] => <li><strong>Impacted</strong>: 2</li> 
    ) 

[6] => Group Object 
    (
     [_title] => <strong>Feathering</strong><ul> 
     [_issues] => <li><strong>Soiled</strong>: 1</li> 
    ) 

[7] => Group Object 
    (
     [_title] => <strong>Feet</strong><ul> 
     [_issues] => <li><strong>Swollen</strong>: 1</li> 
    ) 

[8] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[9] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[10] => Group Object 
    (
     [_title] => <strong>Heart</strong><ul> 
     [_issues] => <li><strong>Compromised</strong>: 1</li> 
    ) 

[11] => Group Object 
    (
     [_title] => <strong>Hock</strong><ul> 
     [_issues] => <li><strong>Clear Exudate</strong>: 2</li> 
    ) 

[12] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[13] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[14] => Group Object 
    (
     [_title] => 
     [_issues] => 
    ) 

[15] => Group Object 
    (
     [_title] => <strong>Kidney</strong><ul> 
     [_issues] => <li><strong>Injured</strong>: 2</li> 
    ) 

) 

的foreach()循環該清除出空的對象陣列的內部:

foreach ($issues as $key => $link) { 
      if ($issues[$key] != '') { 
     unset($issues[$key]); 
    } 
    } 

最後,循環運行時打印的輸出:

<strong>Abdomen</strong><ul> 
    <li><strong>Ascites</strong>: 1</li> 
    <li><strong>Blood Clot</strong>: 2</li> 
<strong>Air Sac</strong><ul> 
    <li><strong>Cloudy</strong>: 1</li> 
<strong>Beak Treatment</strong><ul> 
    <li><strong>Severe</strong>: 3</li> 
<strong>Crop</strong><ul> 
    <li><strong>Impacted</strong>: 2</li> 
<strong>Feathering</strong><ul> 
    <li><strong>Soiled</strong>: 1</li> 
<strong>Feet</strong><ul> 
    <li><strong>Swollen</strong>: 1</li> 
<strong>Heart</strong><ul> 
    <li><strong>Compromised</strong>: 1</li> 
<strong>Hock</strong><ul> 
    <li><strong>Clear Exudate</strong>: 2</li> 
<strong>Kidney</strong><ul> 
    <li><strong>Injured</strong>: 2</li> 

顯然我只想要空的組ps被刪除,沒有任何要打印的東西。任何幫助表示讚賞。

+2

這意味着,你需要向我們展示更多的代碼,根據您提供的代碼,這是不可能會有一個輸出... – HamZa

回答

1

至於有人建議here,您可以使用array_filter

class Foo { 

    public $bar; 

    public function __construct($arg) 
    { 
     $this->bar = $arg; 
    } 

    public function isEmpty() 
    { 
     if (is_string($this->bar) && !empty($this->bar)) 
     { 
      return false; 
     } 

     return true; 
    } 

} 

$array = array(
    new Foo(29), 
    new Foo(''), 
    new Foo('hello'), 
    new Foo(null), 
    new Foo('world'), 
); 

function obj_is_empty($obj) 
{ 
    return (! $obj->isEmpty()); 
} 

print "<pre>"; 
var_dump(array_filter($array, "obj_is_empty")); 

做工精細

0

我可能是錯的,但你的陣列應該有引號,項目:

[_title] => "<strong>Abdomen</strong><ul>" 

否則無論未來是不可預知的。

0

您正在檢查數組keys(_title,_issues),而不是values(Abdomem等)。