php和xml中的if in_数组

php和xml中的if in_数组,php,xml,arrays,Php,Xml,Arrays,我有一个包含500多个项目的长xml数据文件,其形式如下: <?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <ITEM> <TITLE>ITEM name</TITLE> <TYPE>"T1", "T2", "T3" </TYPE> <DESCIPTION>DESCIPTIONiliate Page CPM</D

我有一个包含500多个项目的长xml数据文件,其形式如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<ITEM>
    <TITLE>ITEM name</TITLE>
    <TYPE>"T1", "T2", "T3" </TYPE>
    <DESCIPTION>DESCIPTIONiliate Page CPM</DESCIPTION>
    <PRICE>PRICE</PRICE>
    <ITEM>http://mysite.com/item-link</ITEM>
</ITEM>
</CATALOG>

项目名称
“T1”、“T2”、“T3”
描述说明页CPM
价格
http://mysite.com/item-link
我在php页面中使用以下代码从xml文件导入数据:

<?php
$ITEMSS = new SimpleXMLElement('ITEMS.xml', null, true);

echo <<<EOF
<table width="100%" align="center" border="1" bordercolor="#0099ff" cellpadding="1" cellspacing="0">

    <tr>
            <th bgcolor="#66ccff"><span class="style4">ITEM Name</span></th>
            <th bgcolor="#66ccff"><span class="style4">item TYPE </span></th>
            <th bgcolor="#66ccff"><span class="style4">item DESCIPTION </span></th>
            <th bgcolor="#66ccff"><span class="style4">item PRICE</span></th>
            <th bgcolor="#66ccff"><span class="style4">link to item</span></th>
    </tr>

EOF;
foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
    echo <<<EOF
    <tr height="30" align=middle>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><span class="STYLE7">{$ITEMS->TITLE}</span></a></td>
            <td><span class="STYLE8">{$ITEMS->TYPE}</span></td>
            <td><span class="STYLE8">{$ITEMS->DESCIPTION}</span></td>
            <td><span class="STYLE8">{$ITEMS->PRICE}</span></td>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><B><span class="STYLE7">cHECK IT OUT</span></B></a></td>
    </tr>

EOF;
}
echo '</table>';
?>

问题来自于$options变量的准备,请尝试这样启动它

PHP>=5.3

$options = array_map(function($type) {
    return str_replace('"', '', trim($type));
}, explode(',', $ITEMS->TYPE));
PHP<5.3(将函数置于循环之外:D)


这样可以将类型分解为一个数组,删除引号并修剪额外的空格

不要在
foreach
循环中放置
if
子句,只需将另一个列表传递到
foreach
中,该列表只包含要查找的元素。工作起来很神奇,实际上这叫做xpath:

就这样。这是因为您可能要查找的每个值都包含在双引号中,这是一个常见的原则


顺便说一句,我认为你的代码会从一个简单的模板引擎中获益匪浅,比如说。它与迭代器(你用
foreach
迭代的值列表)一起工作。

用我想要的
值“
,你能举一个例子来说明你会在那里得到什么吗?那不是一个数组,而是一个字符串,所以数组中的
没有多大意义。最好先隔离问题,你的问题会好得多,解决得更快。我想要的值可以是T1,T2…,我想在类型为T1的页面上显示数据,在类型为T3的其他页面上显示数据example@hakra你是什么意思?那么如何使用if语句检查给定类型列表中是否存在值呢?请添加
var\u dump($ITEMS->TYPE)的输出到你的问题。我得到了这个错误:“意外的T_函数,期望'”,这行是为“$options=array_map(FUNCTION($type){”添加了一个<5.3的示例,用于不使用closure谢谢Jason Brumwell,它很完美,我有PHP5.2.17,这就是它不起作用的原因:)
foreach($ITEMSS as $ITEMS) // loop through our DATAS
{
$options = array($ITEMS->TYPE);

if (in_array("T1", $options))

   { echo <<<EOF
    <tr height="30" align=middle>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><span class="STYLE7">{$ITEMS->TITLE}</span></a></td>
            <td><span class="STYLE8">{$ITEMS->TYPE}</span></td>
            <td><span class="STYLE8">{$ITEMS->DESCIPTION}</span></td>
            <td><span class="STYLE8">{$ITEMS->PRICE}</span></td>
            <td><a href="{$ITEMS->ITEM}" target="_blank"><B><span class="STYLE7">cHECK IT OUT</span></B></a></td>
    </tr>


EOF;
}

}
$options = array_map(function($type) {
    return str_replace('"', '', trim($type));
}, explode(',', $ITEMS->TYPE));
function parseTypes($type) {
    return str_replace('"', '', trim($type));
}

$options = array_map(parseTypes, explode(',', $ITEMS->TYPE));
$value = 'T1';
$list  = $ITEMSS->xpath('/CATALOG/ITEM[contains(TYPE, \'"' . $value . '"\')]');
foreach($list as $ITEMS)
{
    ...