[PHP] Pulling unique data from database into an array
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 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[2]: [PHP] Pulling unique data from database into an array
Hello Jochem, I tried what you suggested and it work just perfect as long as i made the array by hand. But when I try to generate the array using the database I can not get it to work. How would I create the array $arrX from the mysql database. I have tried this but it does not work properly while (!$recordSet-EOF) { $arrX = $recordSet-fields['user_id'] = $recordSet-fields['field_value']; $recordSet-MoveNext(); $arrX = array( 6 = English||Spanish, 8 = English||Portuguese||Finnish, 2 = English ); // 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 (userslang\n); foreach ($arrFinal as $lang = $users) { echo implode(',',$users). $lang\n; } Monday, February 2, 2004, 12:47:25 PM, you wrote: JM // given an array in the form: JM $arrX = array( '' = 'English||Spanish' '8' = 'English||Portuguese||Finnish' JM // ...etc... JM ); JM // define a new array loop thru $arrX like so: JM $arrTmp = array() JM foreach ($arrX as $langs) { JM $arrTmp = array_merge($arrTmp, explode('||',$langs)); JM } JM // now loop thru the $arrTmp array (and for each item loop the original JM $arrX array) as follows: JM foreach ($arrTmp as $Tmp) { JM $arrFinal[$Tmp] = array(); JM foreach($arrX as $uid = $langs) { JM // is the lang in this users lang list JM if (stristr($langs, $Tmp)) { JM // yes it is! add the user to this lang JM $arrFinal[$Tmp][] = $uid; JM } JM } JM } JM // now dump the results to the screen: JM echo (userslang\n); JM foreach ($arrFinal as $lang = $users) { JM echo implode(',',$users). $lang\n; JM } JM --- JM that was off the top of my head; there is probably a better way of doing JM it but hopefully it points you in the right direction. JM if you don't understand this code I suggest testing it out (there may be JM typos!!) - these are basic manipulations and a developing proper JM understanding of them is very important to being able to write good code JM - I speak from experience when I say the only real way to understand it JM is to play with it. JM have fun. JM 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); -- Best regards, Richardmailto:[EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: Re[2]: [PHP] Pulling unique data from database into an array
[snip] while (!$recordSet-EOF) { $arrX = $recordSet-fields['user_id'] = $recordSet-fields['field_value']; $recordSet-MoveNext(); [/snip] while (!$recordSet-EOF) { $arrX[$recordSet-fields['user_id']] = $recordSet-fields['field_value']; $recordSet-MoveNext(); } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php