【问题标题】:Parsing empty XML elements in PHP在 PHP 中解析空的 XML 元素
【发布时间】:2013-09-13 00:46:06
【问题描述】:

PHP 新手在这里。我正在解析一个长 XML 文件,将每个标签设置为一个变量。如果元素的标签为空,我想给它赋值“N/A”

我想知道是否有比我目前的方法更简洁的方法:

$elements = array()

$propertyOwner = $report->PropertyProfile->PrimaryOwnerName[0];
array[] = $propertyOwner;
$propertyAddress = $report->PropertyProfile->SiteAddress[0];
array[] = $propertyAddress;
...
for($i=0; $i<count($elements); $i++) {
    if (array[i] === '') {
        array[i] = 'N/A');
    }
}

【问题讨论】:

    标签: php xml parsing xml-parsing


    【解决方案1】:

    最初编写代码的方式(每个变量前面没有$s)您可能整天都会遇到错误。希望这有助于简化:

    $array[] = '';
    $elements = array();
    $propertyOwner = $report->PropertyProfile->PrimaryOwnerName[0];
    $elements[] = $propertyOwner;
    $propertyAddress = $report->PropertyProfile->SiteAddress[0];
    $elements[] = $propertyAddress;
    //...
    //         IF                  THEN        ELSE
    $array[] = !empty($elements) ? $elements : 'N/A';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多