我正在从XML文件导出数据以更新我的数据库。我使用以下循环:

foreach ($xml->removed->idof as $idof) {
  if ($idof > 0) {
    mysql_query("delete from wp_posts WHERE import_id = '$idof'");
    echo $idof;
  }
}

我的数据结构如下:
<removed>
<idof mlssta='4'>0</idof>
<idof mlssta='6'>60370</idof>
<idof mlssta='14'>150370</idof>
<idof mlssta='6'>150671</idof>
...
</removed>

我需要更改if条件以检查mlstaa属性的值,即if ($idof > 0 && mlstaa != 6)
我只是不知道怎么提取。非常感谢你的帮助。

最佳答案

使用$xml变量看起来像是用simplexml扩展加载的。假设它是这样初始化的:

$xml = simplexml_load_file('some-file.xml');

然后您只需使用attributes()获取属性,并检查该属性是否在返回的数组中:
foreach ($xml as $idof) {
  $attr = $idof->attributes();
  if ($attr && $attr['mlssta'] != 6) {
    // remove it here
  }
}

10-02 09:24