The code below is being used to retrieve the value of the "store" element from the XML file below and insert the values into an array (the storeArray). I do NOT want duplicate values put into the array (IE I don't want Best Buy inserted twice), so I am using the in_array method to prevent duplication.
This code works fine:
$xmlDoc = simplexml_load_file("products.xml"); $storeArray = array();
foreach($xmlDoc->product as $Product) {
echo "Name: " . $Product->name . ", ";
echo "Price: " . $Product->price . ", ";
if( !in_array( (string)$Product->store, $storeArray )) {
$storeArray[] = (string)$Product->store;
}}
foreach ($storeArray as $store) {
echo $store . "<br>";
}
But when I try to put those array values (from the XML store element) into a link (like below), the values are duplicated (IE Best Buy is displayed twice. Any advice?
if( !in_array( (string)$Product->store, $storeArray )) {
$storeArray[] = "<a href='myLink.htm'>" . (string)$Product->store . "</a>";
foreach ($storeArray as $store) {
echo $store . "<br>";
}
Here is the XML file:
<product type="Electronics">
<name> Desktop</name>
<price>499.99</price>
<store>Best Buy</store>
</product>
<product type="Electronics">
<name>Lap top</name>
<price>599.99</price>
<store>Best Buy</store>
</product>
<product type="Hardware">
<name>Hand Saw</name>
<price>99.99</price>
<store>Lowes</store>
</product>
</products>