Loading first occurance of item in array

I'm using SimpleXML to parse a rss feed and if an attribute in returned arrays by SimpleXML matches with my string show data. but some items are duplicated in XML file and I want to use just their first occurrence. but I'm getting all the duplicated results.

foreach($feed->city as $city){
    if(in_array($city['Name'], $cities)){
        echo $city['Name'].'<br />';
    }
}

where $feed->city is my XML data and $cities is my array.

Answers


I would use php's Xpath library to do this. Xpath 2.0 could do this directly, but the built in Xpath 1.0 language still gets you very close.

    <!-- language: lang-php -->
$xmlDocument = new DOMDocument();
    if ($xmlDocument->load('file.xml')) {
    $xpath = new DOMXPath($xmlDocument);
    $nodeList = $xpath->query('//city/@Name', $xmlDocument);

from here you have a DOMNodeList class. throw it in an array and remove duplicates. I don't ahve a php environment around right now to finish it up, but I wanted to offer a different direction than just simplXML by itself.


Need Your Help

Angular JS with jquery masonry

angularjs angularjs-directive jquery-masonry

I need to use angular and masonry and also implement sorting and filtering.

Prevent MySQL-Python from inserting quotes around database name parameter

python mysql django mysql-python

I'm working on a project that requires me to programmatically create MySQL users from a django app. I can create the users just fine:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.