使用php按日期过滤xml数据

时间:2013-02-08 06:22:06

标签: php xml date xml-parsing

我想使用php

按日期过滤我的xml数据
<xml>
<customer>
  <cid>1</cid>
  <amount_paid>1000</amount_paid>
  <date>2012-01-10</date>
</customer>
<customer>
  <cid>2</cid>
  <amount_paid>3000</amount_paid>
  <date>2012-01-10</date>
</customer>
<customer>
  <cid>3</cid>
  <amount_paid>1000</amount_paid>
  <date>2012-01-05</date>
</customer>
<customer>
  <cid>6</cid>
  <amount_paid>7000</amount_paid>
  <date>2012-01-21</date>
</customer>
</xml>

如何按日期过滤我的xml? 例如。我想让客户在date =“2012-01-10”

1 个答案:

答案 0 :(得分:1)

See it in action

<?php
    $string = '<xml>
<customer>
  <cid>1</cid>
  <amount_paid>1000</amount_paid>
  <date>2012-01-10</date>
</customer>
<customer>
  <cid>2</cid>
  <amount_paid>3000</amount_paid>
  <date>2012-01-10</date>
</customer>
<customer>
  <cid>3</cid>
  <amount_paid>1000</amount_paid>
  <date>2012-01-05</date>
</customer>
<customer>
  <cid>6</cid>
  <amount_paid>7000</amount_paid>
  <date>2012-01-21</date>
</customer>
</xml>';

$xml = simplexml_load_string($string);

foreach ($xml->customer AS $customer)
{
    if ($customer->date == '2012-01-10')
    {
        echo $customer->cid . "<br>\n";
        echo $customer->amount_paid . "<br>\n";
    }
}