如何在php explode数组中处理空值?

时间:2015-08-10 06:32:05

标签: php mysql arrays

我正在尝试创建一个具有特定<th>值的表格,我的数据如下:

Field Name, Field Value;Field Name, Field Value;Field Name, Field Value;

我在这样的foreach循环中使用了两个爆炸。

$custom_fields = $row{'custom_fields'};

$fields = explode(";", $custom_fields);

foreach ($fields as $field) {
    $cf = explode(',', $field);

    echo "<td>".$cf[1]."</td>";
}

这将根据需要输出数据,但数组中的值的数量会有所不同(总共有34个可能的值)。如何让空行显示没有值,或将它们留空?

我在寻找:

<table class="">
    <thead>
        <tr>
            <th nowrap>Choose your plan</th>
            <th nowrap>Month</th>
            <th nowrap>Day</th>
            <th nowrap>Year</th>
            <th nowrap>Street Address</th>
            <th nowrap>City</th>
            <th nowrap>State</th>
            <th nowrap>Zip</th>
            <th nowrap>Phone</th>
            <th nowrap>Alternate Phone</th>
            <th nowrap>Email Address</th>
            <th nowrap>Provider ID #</th>
            <th nowrap>I have read and understand the Terms and Conditions</th>
            <th nowrap>Total Plan Members</th>
            <th nowrap>Family Member 2 First Name</th>
            <th nowrap>Family Member 2 Last Name</th>
            <th nowrap>Family Member 3 First Name</th>
            <th nowrap>Family Member 3 Last Name</th>
            <th nowrap>Family Member 4 First Name</th>
            <th nowrap>Family Member 4 Last Name</th>
            <th nowrap>Family Member 5 First Name</th>
            <th nowrap>Family Member 5 Last Name</th>
            <th nowrap>Family Member 6 First Name</th>
            <th nowrap>Family Member 6 Last Name</th>
            <th nowrap>Family Member 7 First Name</th>
            <th nowrap>Family Member 7 Last Name</th>
            <th nowrap>Family Member 8 First Name</th>
            <th nowrap>Family Member 8 Last Name</th>
            <th nowrap>Family Member 9 First Name</th>
            <th nowrap>Family Member 9 Last Name</th>
            <th nowrap>Family Member 10 First Name</th>
            <th nowrap>Family Member 10 Last Name</th>
            <th nowrap>Date of Birth (dd/mm/yyyy)</th>
            <th nowrap>Billing same as Shipping</th>
        </tr>
    </thead>
    <tbody>
    <tr>
    $custom_fields = $row{'custom_fields'};

    $fields = explode(";", $custom_fields);

    foreach ($fields as $field) {
        $cf = explode(',', $field);

        echo "<td>".$cf[1]."</td>";
    }
    </tr>
</tbody>
</table>

1 个答案:

答案 0 :(得分:1)

试试这个:

$custom_fields = $row{'custom_fields'};
$fields = explode(";", $custom_fields);
foreach ($fields as $field) {
  $cf = explode(',', $field);
  if(count($cf)== 2)
  {
     echo "<td>".$cf[1]."</td>";
  }
  else
  {
     echo "<td>No Value</td>";
  }
}