centenial Posted March 19, 2007 Share Posted March 19, 2007 Hi, I have a string, like this: Userid1 Pass1 Userid2 Pass2 Userid3 Pass3 Userid4 Pass4 Each column is tab separated. I want to explode it into an array so that the userid will be the key, and the password the value. I tried to do it like this: $array = explode("\t", $string); // Where $string is the string above. However, this returned both the userid and the password as the value, and created an automatic key for each row. Can anyone help me? Thanks, Quote Link to comment Share on other sites More sharing options...
fert Posted March 19, 2007 Share Posted March 19, 2007 foreach($array as $a) { $temp=explode(" ",$a); $array2[$temp[0]]=$temp[1]; } $array=$array2; Quote Link to comment Share on other sites More sharing options...
centenial Posted March 19, 2007 Author Share Posted March 19, 2007 Hi, Thanks for the quick reply. That code returned this: Array ( [userid1] => [Pass1 Userid2] => [Pass2 Userid3] => [Pass3 Userid4] => [Pass4 ] => ) Which doesn't look right. Any other ideas? Quote Link to comment Share on other sites More sharing options...
drewbee Posted March 19, 2007 Share Posted March 19, 2007 $otherArray = array(); foreach ($array AS $a) { $temp = explode(" ",$a); $otherArray[$temp['0']] = $temp['1']; } echo print_r($otherArray); If that returns the same thing as above, try splitting the original array with the "\n" operator instead of "\t" Quote Link to comment Share on other sites More sharing options...
Barand Posted March 19, 2007 Share Posted March 19, 2007 <?php $lines = explode("\n", $str); $array = array(); foreach ($lines as $line) { list($u, $p) = explode("\t", $line); $array[$u] = $p; } echo '<pre>', print_r($array, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.