Re: [PHP] Pulling unique data from database into an array
// given an array in the form: $arrX = array( '6' = 'English||Spanish' '8' = 'English||Portuguese||Finnish' // ...etc... ); // define a new array loop thru $arrX like so: $arrTmp = array() foreach ($arrX as $langs) { $arrTmp = array_merge($arrTmp, explode('||',$langs)); } // now loop thru the $arrTmp array (and for each item loop the original $arrX array) as follows: foreach ($arrTmp as $Tmp) { $arrFinal[$Tmp] = array(); foreach($arrX as $uid = $langs) { // is the lang in this users lang list if (stristr($langs, $Tmp)) { // yes it is! add the user to this lang $arrFinal[$Tmp][] = $uid; } } } // now dump the results to the screen: echo (users lang\n); foreach ($arrFinal as $lang = $users) { echo implode(',',$users). $lang\n; } --- that was off the top of my head; there is probably a better way of doing it but hopefully it points you in the right direction. if you don't understand this code I suggest testing it out (there may be typos!!) - these are basic manipulations and a developing proper understanding of them is very important to being able to write good code - I speak from experience when I say the only real way to understand it is to play with it. have fun. Richard Kurth wrote: I am pulling data from a database that list the Language a person speaks and the id number. It is in a format like this. user_idfield_value 6English||Spanish 2English 8English||Portuguese||Finnish 5English||Japanese||German 3English 1English 9German each time it looks at a new record it list all the languages that that person speaks with a double pipe in between each language. What I need to do is find all the unique languages so I can generate a list of languages that do not have any repeats in it with each users id number that speaks that language. so the list above would be 6,2,8,5,3,1 English 8 Portuguese 8 Finnish 5 Japanese 5,9 German 6 Spanish Using the script below (I am using ADODB for a database layer) I am able to pull the data into an array of both fields so it gives me an array that looks like this Array ( [6] = English||Portuguese||Finnish [2] = English||Portuguese||German [8] = English [7] = English ) ) But I can not figure out how to split it up so there is a list like I have above. I know I need to use explode, array_merge and array_unique but I am not sure how to set it up so it will work correctly $sql = SELECT * FROM default_UserDBElements WHERE field_name = 'Languages'; $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $recordSet = $conn-Execute($sql); if (!$recordSet) print $conn-ErrorMsg(); else while (!$recordSet-EOF) { $array[$recordSet-fields['user_id']] = $recordSet-fields['field_value']; $recordSet-MoveNext(); } print_r($array); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pulling unique data from database
On Monday 02 February 2004 10:26, Richard Kurth wrote: What I need to do is find all the unique languages so I can generate a list of languages that do not have any repeats in it. so the list above would be English,Portuguese,Finnish,Japanese,German,Spanish in any order explode() and array_unique() or array_flip() should get you started. -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* A fool must now and then be right by chance. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pulling unique data from database
Hello Richard, Normally the distinct calause in sql serves this purpose but it seems that the way your DB is designed you cannot make use of it. The bad news is that you might have to read all the rows split them by the pipe symbol and insert them into a list manually. ouch! Richard Kurth wrote: I am pulling data from a database that list the Language a person speaks. It is in a format like this. English||Spanish English English||Portuguese||Finnish English||Japanese||German English English German each time it looks at a new record it list all the languages that that person speaks with a double pipe in between each language. What I need to do is find all the unique languages so I can generate a list of languages that do not have any repeats in it. so the list above would be English,Portuguese,Finnish,Japanese,German,Spanish in any order How would I go about this. I can not change the format of the database and there are over 200 people listed in the database. -- Raditha Dissanayake. http://www.radinks.com/sftp/ | http://www.raditha.com/megaupload Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Pulling unique data from database
Here's some skeleton code showing one way (untested, no error checking shown): $query = 'SELECT Language FROM whatever'; $result = mysql_query($query); $languages = array(); while ($row=mysql_fetch_object($result)) $languages = array_merge($languages, explode('||', $row-Language)); $languages = array_unique($languages); -- Jonathan Rosenberg President Founder, Tabby's Place http://www.tabbysplace.org/ -Original Message- From: Richard Kurth [mailto:[EMAIL PROTECTED] Sent: Sunday, February 01, 2004 9:27 PM To: [EMAIL PROTECTED] Subject: [PHP] Pulling unique data from database I am pulling data from a database that list the Language a person speaks. It is in a format like this. English||Spanish English English||Portuguese||Finnish English||Japanese||German English English German each time it looks at a new record it list all the languages that that person speaks with a double pipe in between each language. What I need to do is find all the unique languages so I can generate a list of languages that do not have any repeats in it. so the list above would be English,Portuguese,Finnish,Japanese,German,Spanish in any order How would I go about this. I can not change the format of the database and there are over 200 people listed in the database. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Pulling unique data from database
On Sun, 2004-02-01 at 21:26, Richard Kurth wrote: each time it looks at a new record it list all the languages that that person speaks with a double pipe in between each language. What I need to do is find all the unique languages so I can generate a list of languages that do not have any repeats in it. so the list above would be English,Portuguese,Finnish,Japanese,German,Spanish in any order Use explode (http://www.php.net/explode) to split the string and put it in an array for each row; joining them together to make one big array. Then use array_unique (http://www.php.net/array_unique) to remove the dupes. -- Adam Bregenzer [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php