Reputation: 17
Learning how to use PHP playing around with a test store generating a grid of products from XML data.
Question: I want to make a filter and i'm struggling with taking the data from the XML and removing duplicate category values.
Below is a template of the XML:
<products>
<product>
<id>0</id>
<url>product-image.jpg</url>
<category>Ferraro</category>
<price>$10.00</price>
<rating>0</rating>
<comment>NA</comment>
</product>
My attempt of working on the php
function removeDup()
{
$xml = simplexml_load_file("data.xml") or die("Error: Cannot create object");
$seen = array();
$productCount = $xml->product->count();
for ($i = 0; $i < $productCount; $i++) {
$seen[] = $xml->product[$i]->category;
// $seen[$i];
}
echo array_unique($seen);
Upvotes: 0
Views: 206
Reputation: 146450
First of all, some remarks in no particular order:
You're looping elements in a pretty convoluted way. SimpleXML objects are iterable when applies, so you can use a simple foreach
construct.
array_unique() returns an array and you cannot echo
arrays. If, as it seems, you aren't getting PHP Notice: Array to string conversion from PHP that means that you haven't configured your PHP environment to report errors. As quick start, you can set the error_reporting
and display_errors
directives in your computer's system-wide php.ini
file (details here).
SimpleXML methods typically returns objects (instances of the SimpleXMLElement
class). You often want to force a cast to string, or whatever other type you need, when it doesn't happen automatically.
Said that, your code works fine as far as I can tell. Both your XML and PHP samples are incomplete so it's possible that the error is somewhere else.
In any case, another possible approach would be to generate a key for the array using the unique field (or fields). That way, each new duplicate element automatically overwrites the previous one. This has the added benefit of being able to retrieve additional fields which may not be identical:
<products>
<product>
<id>0</id>
<url>product-image.jpg</url>
<category>Ferraro</category>
<price>$10.00</price>
<rating>0</rating>
<comment>NA</comment>
</product>
<product>
<id>1</id>
<url>product-image.jpg</url>
<category>Ferraro</category>
<price>$10.00</price>
<rating>0</rating>
<comment>NA</comment>
</product>
<product>
<id>2</id>
<url>product-image.jpg</url>
<category>Another one</category>
<price>$10.00</price>
<rating>0</rating>
<comment>NA</comment>
</product>
</products>
$xml = simplexml_load_file("data.xml") or die("Error: could not load data");
$seen = [];
foreach ($xml->product as $product) {
// Just an example to ilustrate the posibilites
$category = (string)$product->category;
$product_count = $seen[$category]['product_count'] ?? 0;
$seen[$category] = [
'category_name' => $category,
'product_count' => $product_count + 1,
];
}
var_dump($seen);
array(2) {
["Ferraro"]=>
array(2) {
["category_name"]=>
string(7) "Ferraro"
["product_count"]=>
int(2)
}
["Another one"]=>
array(2) {
["category_name"]=>
string(11) "Another one"
["product_count"]=>
int(1)
}
}
Upvotes: 1