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 和 lname 有值,那么邮件将是

First name = asdff
Last Name= adsf

示例 2:如果只有 fname,lname 和 address 有值,那么邮件将是

 First name = asdff
    Last Name= adsf
Address=sdfaf
4

3 回答 3

3
$array = array(); // your array that you posted...
$new_array = array();

foreach ($array as $key => $val) {
    if ( ! empty($val) ) $new_array[$key] = $val;
}
于 2013-01-24T05:46:35.700 回答
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
)

愿这对你有所帮助。

于 2013-01-24T05:52:10.740 回答
1
$mail = '';
foreach ($array as $name => $value) {
   if ($value) {
      $mail .= "$name = $value \n";
   }
}
于 2013-01-24T05:46:32.233 回答