简体   繁体   中英

Filter xml data by date using php

I want to filter my xml data by date using php

<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>

How can filter my xml by date? For example. I want get customers where date = "2012-01-10"

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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM