2012-11-28 62 views
0

我有這樣環路雖然陣列,看看是否一個元素是空白的,輸出的HTML,如果不爲空白

Array 
(
[0] => Array 
    (
     [id] => 139 
     [name] => Eithne Maun 
     [contactNo] => 877692398 
     [email] => [email protected] 
     [address] => 
     [county] => Cork 
     [country] => Ireland 
     [sourceOfContact] => Phone 
     [type] => Corporate 
     [dateAdded] => 2012-11-26 
     [companyName] => 
     [dateOfBirth] => 
    ) 

陣列)

欲通過陣列和僅顯示在每個元件循環下面的行如果值不是空的

Number: <?php htmlout($contact[0]['contactNo']);?><br /> 
Email: <?php htmlout($contact[0]['email']);?><br /> 
Address: <?php htmlout($contact[0]['address']);?><br /> 
County: <?php htmlout($contact[0]['county']);?><br /> 
Country: <?php htmlout($contact[0]['country']);?><br /> 
Type of Contact: <?php htmlout($contact[0]['type']);?><br /> 
Company Name: <?php htmlout($contact[0]['companyName']);?><br /> 
Date of Birth: <?php htmlout($contact[0]['dateOfBirth']);?><br /> 

是否有一個函數,我可以使用循環?

回答

0

嘗試

<?php 
if(!empty($contact[0]['contactNo'])) { ?> 
    Number: <?php htmlout($contact[0]['contactNo']);?><br /> 
<?php } ?> 
1
$fields = array(
'Number' => 'contactNo', 
'Email' => 'email', 
'Address' => 'address', 
'County' => 'county', 
'Country' => 'country', 
'Type of Contact' => 'type', 
'Company Name' => 'companyName', 
'Date of Birth' => 'dateOfBirth'); 

foreach ($fields as $key => $val) 
{ 
    if (!empty($contact[0][$val])) 
    { 
     echo $key . ': ' . $contact[0][$val] . "<br />\n"; 
    } 
} 
+0

好的,謝謝你的幫助 –

+0

做了這個工作o爲你呢? –

+0

已經解決了嗎?如果是這樣,請接受答案,以便將其標記爲已解決。 –

0
$record = array_filter($contact[0]); 

然後遍歷$記錄每個條目,所有的空項將已經被過濾掉了

但要小心爲0的數值是當使用array_filter這樣的時候,我認爲是空的