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

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 php.net/manual/en/simplexmlelement.children.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.