When I pull the categories, I want the same categories not to be listed, but it lists them all. I'll be happy if you can help me. Thanks. XML File
<?xml version="1.0" encoding="UTF-8"?>
-<products>
-<product>
<product_code>AXX-655</product_code>
<product_name>Samsung Led TV</product_name>
<categories>Electronic > Home Electronic > TV</categories>
</product>
-<product>
<product_code>ACC-124</product_code>
<product_name>Fleece Coat</product_name>
<categories>Clothes > Man Clothes > Coat</categories>
</product>
-<product>
<product_code>ADF-147</product_code>
<product_name>LG 2103 Led TV</product_name>
<categories>Electronic > Home Electronic > TV</categories>
</product>
-<product>
<product_code>AXX-655</product_code>
<product_name>Sony Led TV</product_name>
<categories>Electronic > Home Electronic > TV</categories>
</product>
-</products>
PHP file
<?php
$xml = simplexml_load_file('xml/localxmlfile3.xml') or die("Error: Cannot create object");
foreach ($xml->children() as $child) {
$categories = trim($child->categories);
echo $categories."<br>";
}
Output
Electronic > Home Electronic > TV
Clothes > Man Clothes > Coat
Electronic > Home Electronic > TV
Electronic > Home Electronic > TV
I Want
Electronic > Home Electronic > TV
Clothes > Man Clothes > Coat
I intend to get the categories and subcategories separately. But it repeats as much as the number of products. I tried using the array_unique function and it didn't work.
you need to create a helper array to check the category is exist or not (in_array($val, $array_name) //return true or false
)
<?php
$xml = simplexml_load_file('xml/localxmlfile3.xml') or die("Error: Cannot create object");
$category_helper = array();
foreach ($xml->children() as $child) {
$category_name = trim($child->categories);
//if not exist in our helper array we add this
if(!in_array($category_name, $category_helper)) {
$category_helper[] = $category_name;
}
}
var_dump($category_helper);
?>