// 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_id field_value
6 English||Spanish
2 English
8 English||Portuguese||Finnish
5 English||Japanese||German
3 English
1 English
9 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 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



Reply via email to