【发布时间】:2019-11-30 21:30:36
【问题描述】:
我正在研究一种基于它们在 PHP 中的值来过滤某些节点的方法。我正在尝试返回等于“已资助”的状态节点的计数。
我不确定的部分是使用数组(新过滤器 ($xml_bio_children) 中前一个过滤器 ($xml_bio_record[0]) 的结果)。
此代码不返回 $count_c 的计数。我将如何过滤 status="Funded"?感谢您提供任何线索。
这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
<record id="1A">
<congrant>
<ident>a</ident>
<status>Not Funded</status>
</congrant>
<congrant>
<ident>b</ident>
<status>Funded</status>
</congrant>
<congrant>
<ident>c</ident>
<status/>
</congrant>
</record>
<record id="1B">
<congrant>
<ident>a</ident>
<status>Not Funded</status>
</congrant>
<congrant>
<ident>b</ident>
<status>Funded</status>
</congrant>
<congrant>
<ident>c</ident>
<status/>
</congrant>
</record>
<record id="1C">
<congrant>
<ident>aaa</ident>
<status>Funded</status>
</congrant>
<congrant>
<ident>bbb</ident>
<status>Funded</status>
</congrant>
<congrant>
<ident>c</ident>
<status>Funded</status>
</congrant>
</record>
</data>
这里是 PHP:
$url_bio = "test.xml";
$xml_bio = simplexml_load_file($url_bio);
$xml_bio_record=$xml_bio->xpath('/data/record');
$count_a = count($xml_bio_record);
echo '<br>$count_a is...'.$count_a.'<br>';//
foreach($xml_bio_record as $xa){
echo "Found {$xa->status} <br>";
}
$xml_bio_record=$xml_bio->xpath('//record[@id="1A"]');
$count_b = count($xml_bio_record);
echo '<br>$count_b is...'.$count_b.'<br>';//
foreach($xml_bio_record as $xb){
echo "Found {$xb->status} <br>";
}
$xml_bio_children=$xml_bio_record[0]->xpath('/congrant[status="Funded"]');
$count_c = count($xml_bio_children);
echo '<br>$count_c is...'.$count_c.'<br>';//
foreach($xml_bio_children as $xc){
echo "Found {$xc->status} <br>";
}
=======
作为对此的补充,如果我希望设置一个等于$xb->xpath('./congrant[status="Funded"]') 的变量,例如:$xml_congrant_congrant_children=$xb->xpath('./congrant[status="Funded"]'),然后在分页场景中使用索引来循环通过资助结果,那如何实现?
例如
for ($i = $offset; $i < ($offset + $per_page); $i++)
{
$strCongrant_ident = $xml_congrant_congrant_children[$i]['ident'];
}
我之前在分页设置中使用过这个循环的想法,但是在这里让过滤器应用于变量是行不通的。感谢您提供任何线索。
【问题讨论】: