0

属性を持つxmlファイルがあります

XML ドキュメント

<SET name="John" lastname="Steve" city="New York" Country=""/>

これは、4 列の CSV ファイルに変換されます。

CSVファイル

Name,LastName,City,Country
John,Steve,New York,

Country から空のコンテンツを取得し、USA のようなデフォルトのテキストに変換したい

この置換には str_replace を使用します。

問題は、empy var_dump のように、Empty 属性を取得する方法がわからないことです。

ありがとう

変換ファイル。

$filexml = 'file.xml';
    if (file_exists($filexml)){
        $xml = simplexml_load_file($filexml);
$file = fopen('clients.csv', 'w');
        $header = array('Name', 'LastName', 'City', 'Country');
fputcsv($file, $header, ',', '"');
foreach ($xml->SET as $set){
$item = array();
            $value1 = $set->attributes()->name;
            $value2 = $set->attributes()->lastname;
            $value3 = $set->attributes()->city;
            $value4 = $set->attributes()->Country;

$search_country= array('','10','12')
$replace_country = array('USA','Argentina','Canada')
$item[] = $value1;
$item[] = $value2;
$item[] = $value3;
$item[] = str_replace($search_country, $replace_country, $value4);

fputcsv($file, $item, ',', '"');    
        }
        fclose($file);
    }
4

1 に答える 1

1

empty値が空かどうかを確認するために使用します

$value4 = empty($set->attributes()->Country) ? "USA" : $set->attributes()->Country;
于 2012-11-16T20:25:36.130 に答える