SimpleXml conditional

谁说胖子不能爱 提交于 2019-12-11 15:06:41

问题


Hi I need to filter the xml that was given to me base on the following conditions ex. category needs to be Transportation, package needs to be Good and the last one item needs to have been publish today only.

here is the sample xml

<item>
    <category>Transportaion</category>
    <package>Good</package>
    <date>2018-04-17 17:10:46</date>
    <title>Item sample 1</title>
    <description>This is a sample</description>
</item>
<item>
    <category>Wanted</category>
    <package>Good</package>
    <date>2018-04-17 18:03:46</date>
    <title>Item sample 2</title>
    <description>This is a sample</description>
</item>
<item>
    <category>For sale</category>
    <package>Better</package>
    <date>2018-04-16 14:03:46</date>
    <title>Item sample 3</title>
    <description>This is a sample</description>
</item>

I was able to do the 1st two conditions but I'm clueless about the the last condition Include items published today

here is the code I used

$data= simplexml_load_file("easy2.xml");
$today= date('Y-m-d');
foreach ($data->item as $item)
{
    if ($item->category == 'Transportaion' && $item->package == 'Good' && $item->date == '$today')
    {
        echo "Title: " . $item->title . "<br>";
        echo "Category: " . $item->category . "<br>";
    }
}

can someone please advice, many thanks

来源:https://stackoverflow.com/questions/49900752/simplexml-conditional

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!