Reputation: 549
Counting values with the following code turns no results to my array. Would appreciate any help on what I did wrong thanks.
$xml=simplexml_load_file("sitemap.xml");
$arr = array();
foreach($xml->url as $child)
{
if (isset($child->loc)) {
echo "true";
$arr[] = $child->loc;
} else {
echo "error";
echo $child->loc;
}
}
print_r(array_count_values($arr));
Upvotes: 3
Views: 163
Reputation: 549
Solution formed after the answer of jack. Added this code to check for duplicates.
if(count($arr) != count(array_unique($arr))){
echo "Duplicates";
}
Upvotes: 0
Reputation: 539
May be expecting like this
$xml=simplexml_load_file("sitemap.xml");
echo "<pre>";
var_dump($xml->url->loc[0]);
var_dump($xml->url->loc[1]);
var_dump($xml->url->loc[2]);
echo "</pre>";
$arr = array();
foreach($xml->url as $child)
{
foreach($child as $tmp){
if (isset($tmp)) {
echo "true";
$arr[] = (String)$child->loc;
} else {
echo "error<br/>";
echo $child->loc;
}
}
}
print_r(array_count_values($arr));
<sites>
<url>
<loc>google.com</loc>
<loc>google.com</loc>
<loc>yahoo.com</loc>
</url>
</sites>
Upvotes: 0
Reputation: 173662
You have to cast the item values properly, otherwise you would be storing SimpleXMLElement objects in your array:
$arr[] = (string)$child->loc;
Upvotes: 3