2013-01-24 73 views
1

我有一個數組:傳參數有條件地起作用

<pre>Array 
(
    [fname] => adsff 
    [lname] => adsf 
    [address] => sdfaf 
    [city] => sdaf 
    [state] => dsfaf 
    [zip] => adf 
    [phone] => asdf 
    [fax] => adsf 
    [email] => asdff 
    [salary] => asdaf 
    [pemp] => asdfaf 
    [noofyears] => asdfaf 
    [married] => asdfaf 
    [children] => asdfaf 
    [refname1] => asdfaf 
    [refphone1] => adsfaf 
    [refname2] => asdfaf 
    [refphone2] => sdfaf 
    [refname3] => dfsg 
    [refphone3] => sdf 
    [bank] => sdf 
) 

我想通過這個陣列分成郵件,使得如果任何字段爲空白它然後它不是在郵件 使用例如: 如果僅FNAME和L-NAME中有值,則郵件將

First name = asdff 
Last Name= adsf 

實施例2: 如果只有FNAME,L-NAME和地址中有值,則郵件將是

First name = asdff 
    Last Name= adsf 
Address=sdfaf 

回答

1
$mail = ''; 
foreach ($array as $name => $value) { 
    if ($value) { 
     $mail .= "$name = $value \n"; 
    } 
} 
3
$array = array(); // your array that you posted... 
$new_array = array(); 

foreach ($array as $key => $val) { 
    if (! empty($val)) $new_array[$key] = $val; 
} 
+0

感謝您的邏輯.. – user1986787

1

我認爲使用array_filter

<?php 

$entry = array(
      0 => 'foo', 
      1 => false, 
      2 => -1, 
      3 => null, 
      4 => '' 
     ); 

print_r(array_filter($entry)); 
?> 

The above example will output: 

Array 
(
    [0] => foo 
    [2] => -1 
) 

願這幫助你。