Page 1 of 1

Need to get a unique record from an array

Posted: Mon Jan 18, 2010 2:56 am
by salibaray
Hi there,

I'am developing a snippet for a website widget that will read/fetch data from an XML file, sort it in array's and then show it back to the user - formated as a sentence.

Example:

XML sample:

Code: Select all

 
<row id="1">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>5000</won>
</row>
<row id="2">
<username>GGR1111</username>
<name>Isabelle</name>
<surname>Bonnici</surname>
<game>Lotto</game>
<won>1000</won>
</row>
<row id="3">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>2000</won>
</row>
<row id="4">
<username>GGR1024</username>
<name>Raymond</name>
<surname>Saliba</surname>
<game>Lotto</game>
<won>200</won>
</row>
 
So... I read that XML file line by line and put the values in an array, do some functions and then show the formatted data back to the user:

Formatted data example:

Code: Select all

 
Raymond Saliba won $5000 from Lotto
Isabelle Bonnici won $1000 from Lotto
Raymond Saliba won $2000 from Lotto
Raymond Saliba won $200 from Lotto
 
(notice that 'Raymond Saliba' appears 3 times in this example)

now the problem is this -> I need to only show 'Raymond Saliba' once to the user (each person is unique by his username) and the string that I show to the user must be the one that contains the highest ammount won.

Proper output Example:

Code: Select all

 
Raymond Saliba won $5000 from Lotto
Isabelle Bonnici won $1000 from Lotto
 
So the blueprint behind my explenation is something like this:

Code: Select all

 
Person 1 ------> $5000
                 $2000
                 $200
Person 2 ------> $1000
 
(each person can have alot of values, and I need to show that user with the highest value - ONCE)

Sounds easy right ? -> not for me... I'am not a professional developer with PHP but I tend to get along just fine, I have tried various methods for this snippet but so far I didn't find any luck :(
I tried multidimensional arrays, array_unique, and some sorting functions but all ended with no luck at all...

Can someone that understands what I need give me some advice/help please ? preferably with example code using the sample data that I gave to you in this post (because it makes me understand it better, sometimes I'm a slow learner sorry)

Thank you very much and I'm awaiting your reply :)
Raymond Saliba

Re: Need to get a unique record from an array

Posted: Mon Jan 18, 2010 3:20 am
by pbs
You can use array_unique() function.

Re: Need to get a unique record from an array

Posted: Mon Jan 18, 2010 4:31 am
by salibaray
Hi pbs

The PHP documentation mention that array_unique[] function does not work in conjunction to multidimensional arrays, is it true ? and could you give me and easy example of how the function can be used in my scenario ?

Thanks :)

Re: Need to get a unique record from an array

Posted: Mon Jan 18, 2010 4:47 am
by Apollo
Just loop through your original array, building a new array in which you only store names+amounts if the name is not in there (add new entry) or if the amount is higher than what's already in the new array (overwrite that entry).

Re: Need to get a unique record from an array

Posted: Mon Jan 18, 2010 1:51 pm
by McInfo
Another solution, though likely less efficient than Apollo's, is to store the rows in an in-memory database. In a SELECT query, include a "GROUP BY name" clause to condense the names and an "ORDER BY won DESC" clause to sort the rows. SQLite is a good choice ([1][2]). Also, it's fairly easy to pull data from XML documents with SimpleXML.

Edit: This post was recovered from search engine cache.

Re: Need to get a unique record from an array

Posted: Tue Jan 19, 2010 4:09 am
by salibaray
Hi McInfo

I have used in-memory databases in ASP.NET, is it possible to create and use in-memory databases with php ?, yes I've used SimpleXML, its quite good. Then for RSS there's SimplePie which is fairly easy to implement and make use of too.

Thanks for all your help people -> I'm learning and improving step by step :)

Re: Need to get a unique record from an array

Posted: Tue Jan 19, 2010 11:41 am
by McInfo
Probably the easiest way to create an in-memory database in PHP is with SQLite. (I provided links in my last post.)

Example:

Code: Select all

$sqlite = sqlite_open(':memory:');
sqlite_query($sqlite, 'CREATE TABLE...');
sqlite_query($sqlite, 'INSERT...');
$result = sqlite_query($sqlite, 'SELECT...');
$rows = sqlite_fetch_all($result, SQLITE_ASSOC);
print_r($rows);
sqlite_close($sqlite);
There are other ways to access SQLite, such as with the SQLite3 class or PDO_SQLITE driver.

Edit: This post was recovered from search engine cache.

Re: Need to get a unique record from an array

Posted: Wed Jan 20, 2010 2:11 am
by salibaray
Hi McInfo

Thanks for showing me how to create an in memory db with PHP (ive learned something new :) )

I will definatly check it out pretty soon ;)

thanks again