Need to get a unique record from an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
salibaray
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 2:32 am

Need to get a unique record from an array

Post 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
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: Need to get a unique record from an array

Post by pbs »

You can use array_unique() function.
salibaray
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 2:32 am

Re: Need to get a unique record from an array

Post 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 :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Need to get a unique record from an array

Post 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).
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Need to get a unique record from an array

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 4:24 pm, edited 1 time in total.
salibaray
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 2:32 am

Re: Need to get a unique record from an array

Post 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 :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Need to get a unique record from an array

Post 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.
Last edited by McInfo on Thu Jun 17, 2010 4:25 pm, edited 1 time in total.
salibaray
Forum Newbie
Posts: 4
Joined: Mon Jan 18, 2010 2:32 am

Re: Need to get a unique record from an array

Post 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
Post Reply