我有一个包含标记<image_file_name>
的XML文件,此标记重复,有时此值重复,我试图找到<image_file_name>
的唯一实例总值。
$simpleXML = simplexml_load_file("stock_availability.xml");
$uniqueProducts = array();
foreach ($simpleXML->product as $product) {
$image_file_name = $product->image_file_name;
if(in_array($image_file_name, $uniqueProducts)) {
echo 1;
} else {
$uniqueProducts[] = $image_file_name;
echo 2;
}
$image_file_name = null;
}
echo count($uniqueProducts);
count()
返回image_file_name
不是唯一实例的实例总数。
2
也会不断回应,1
永远不会回显。
答案 0 :(得分:0)
我盯着你的代码几分钟,看不出问题。我认为你使用了print_r($ uniqueProducts),并在输出中找到了重复项。
您是否检查过其他重复条目中的空格或案例差异? 尝试使用一些标准化 - 例如使用strtoupper(trim())和引号。
e.g:
$simpleXML = simplexml_load_file("stock_availability.xml");
$uniqueProducts = array();
foreach ($simpleXML->product as $product) {
$image_file_name = $product->image_file_name;
$dup_check=strtoupper(trim($image_file_name));
if(in_array("$dup_check", $uniqueProducts)) {
echo 1;
} else {
$uniqueProducts[] = "$dup_check";
echo 2;
}
$image_file_name = null;
}
print_r($uniqueProducts);
echo "<br>".count($uniqueProducts);
看看这是否有帮助。