我有'简单'的问题: - )
我正在阅读大型XML文件:
$node = new SimpleXMLElement($reader->readOuterXML());
$ node有元素严重性,类...... 我可以像这样比较它们:
if($node->severity==WARNING){
或者像这样展示:
echo $node->severity;
如果提前阅读发布的变量,我想准备什么:
if(isset($_POST['Severity']) && !EMPTY($_POST['Severity'])){
$Severity[]=$_POST['Severity'];
foreach($Severity as $Severityval){
foreach($Severityval as $Severityvalues){
$searchquery .= '($node->Severity==' .$Severityvalues. ') || ';
}
}
}
然后我想在XML阅读器中使用$ searchquery:
if($searchquery){
echo $node->severity;
}
但是,这总是会返回true并显示所有内容: - (
我的变量已正确创建,如果我回显它显示 ($节点 - >严重性== WARNING)
也许屏幕截图可以解释我的问题:
提前致谢
答案 0 :(得分:1)
试试这个:
if(isset($_POST['Severity']) && !EMPTY($_POST['Severity'])) {
$Severity[] = $_POST['Severity'];
$i = 0;
foreach($Severity as $Severityval)
{
foreach($Severityval as $Severityvalues)
{
if ($node->Severity == $Severityvalues) {
$i++;
}
}
}
if ($i > 0) {
echo $node->severity;
}
}
编辑:我完全理解有问题的,如果你已经加载了一个XML,你可以这样做(对于每个选定的节点值 - 示例工作的严重性,这是一个很好的样本):
$root_node = $your_var;
if(isset($_POST['Severity']) && strlen(trim($_POST['Severity'])) > 0) {
$severities_selected = array_values($_POST['Severity']);
$severity = (string) $root_node->severity;
if (in_array($severity, $severities_selected)) {
echo "This XML contains one selected severity (".$severity.")";
}
}
希望这会有所帮助!!