使用XML和PHP按条件计数项目? [英] Count items by condition with XML and PHP?

查看:199
本文介绍了使用XML和PHP按条件计数项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要获取此文件中只有项目的计数 sellingstatus-> sellstate EndedWithSales ONLY,也可以得到 EnededWithoutSales 的计数。

I would like to get the count of only items inside this file with sellingstatus->sellingstate of EndedWithSales ONLY, and perhaps also get the count for EnededWithoutSales.

<sellingStatus>
    <currentPrice currencyId="USD">25.0</currentPrice>
    <convertedCurrentPrice currencyId="USD">25.0</convertedCurrentPrice>
    <bidCount>1</bidCount>
    <sellingState>EndedWithSales</sellingState>
</sellingStatus>

我如何在php中传递这个参数?

How would I go about passing this argument in php?

以下是XML示例的链接: http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt

Here is a link to the sample of XML: http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt.

可以

推荐答案

首先,您需要访问url thu file_get_contents ,请考虑此示例:

First off, you need to access the url thu file_get_contents, Consider this example:

$url = 'http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt';

// access the url and get that file
$contents = file_get_contents($url);

// convert it to an xml object
$contents = simplexml_load_string($contents);

$count = 0; // initialize counter

// loop and search for that
foreach($contents->searchResult->item as $key => $value) {

    if(isset($value->sellingStatus->sellingState) && $value->sellingStatus->sellingState == 'EndedWithSales') {
        // if that key exists and is contains EndedWithSales, increment it
        $count++;
    }
}

// at the end of the loop echo it or whatever you wanted to do
echo "<script>alert('You have $count ocurrances of EndedWithSales in this XML');</script>";

这篇关于使用XML和PHP按条件计数项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆