1

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

Esteban89
  • 671
  • 1
  • 8
  • 20

1 Answers1

1

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.

user602525
  • 3,126
  • 4
  • 25
  • 40
  • Try extracting the values first with the children function of the SimpleXMLElement class. If you can pull out the values into one array, then you can use array_unique on that array http://www.php.net/manual/en/simplexmlelement.children.php – user602525 Dec 16 '13 at 01:20