Question

it's my first time using array_unique and I'm not sure if I'm doing something wrong.

I have the following code:

array_unique($items);
print_r($items);
foreach ($items as $item) {
echo "$item <br />";
}

print_r is returning: Array ( [0] => SimpleXMLElement Object ( [0] => Tirana ) [1] => SimpleXMLElement Object ( [0] => Tirana ) [2] => SimpleXMLElement Object ( [0] => Tirana ) )

echo in loop is returning: Tirana Tirana Tirana

Was it helpful?

Solution

The SimpleXMLElement class says: __toString() Returns text content that is directly in this element. Does not return text content that is inside this element's children.

From the print_r output, it looks like the text is a child of the object. If the text wasn't a child, it appears array_unique would actually work.

array_unique: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. This should work if, as the doc says, the text of the SimpleXMLElement is not a child of the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top