1

I want to extrect all usernames and passwords each from his file and output it nicely. I wrote a code on my appserv 2.5.1 on my computer but only the last loop gave the username output. Tested the code on other machines and it worked perfectly. Dont know what is the problem ...

usernames.txt content :

user1
user2
user3

passwords.txt content :

pass1
pass2
pass3

script content :

$usernames = explode("\n", file_get_contents("usernames.txt"));
$passwords = explode("\n", file_get_contents("passwords.txt"));

foreach( $usernames as $username )
{
    foreach( $passwords as $password )
    {
        echo $username.":".$password."\n";
    }
}

output :

:pass1
:pass2
:pass3
:pass1
:pass2
:pass3
user3:pass1
user3:pass2
user3:pass3
6
  • You should start by debugging. Try to var_dump($usernames) and see if it contains what it is supposed to contain. Commented Dec 31, 2013 at 13:25
  • You have a logic fault. Use only one loop. By using a loop inside a loop you get n*m cycles [n=count usernames; m=count passwords]. What you want is only n cycles (or m). Commented Dec 31, 2013 at 13:26
  • You really want to display a full matrix of every combination of usernames/passwords from these files? 'Cause that's what this code is set up to do. I suspect you're trying to display just the username and the password associated with it. If so, I'd suggest using one for loop. I'd also suggest that you consider not storing usernames and passwords in separate text files... why not store in a database, or if absolutely necessary, in a file separated by a delimiter (e.g., a CSV file). That way, you'd make sure the usernames and passwords were always stored together properly. Commented Dec 31, 2013 at 13:28
  • each array seperretaly working fine with the foreach loop and contatin what is supposed to conatin. i need the code to work like it designed i have my reasons ... no database i am running it from localhost and need it to be in seperate files because this is just a couple of lines of a really big code ... Commented Dec 31, 2013 at 13:29
  • Ok maybe they work fine separately. But did you try var_dump()? Because your code works perfectly fine, the problem might be with the explode(). It is possible than the line separator is a \r\n instead of a \n. That would explain why it works on some machines and not others. Commented Dec 31, 2013 at 13:34

3 Answers 3

7
for ($i=0;$i<count($usernames) && $i<count($password); $i++) {
    echo $usernames[$i].':'.$passwords[$i];
}

But $password[x] must be related to $usernames[x]

Sign up to request clarification or add additional context in comments.

2 Comments

It seems a lot more logical. But I am not sure it addresses the OP's concern, if usernames are not displaying using a foreach, why would they show up with a for?
You have blank lines on your files... you can add something like if ($usernames[$i] == '') continue;
1

There's always those that will say you don't need it (and you often don't) but I tend to use regular expressions whenever I'm parsing these kind of flat files - there's always some quirky character, extra line-break or difference that finds it's way into a text file - be it from transferring servers, restoring backups or simply user-interference. You could also make use of array_combine in this situation if you'd prefer to carrying on using a foreach loop - I know some folks prefer it for readability.

preg_match_all('/\w+/m', file_get_contents('usernames.txt'), $usernames);
preg_match_all('/\w+/m', file_get_contents('passwords.txt'), $passwords);

if(count($usernames[0]) !== count($passwords[0]))
    die('Computer says: mismatch!');  // some resemblance of error handling...

$result = array_combine($usernames[0], $passwords[0]);
foreach($result as $name => $pass)
    echo "{$name}:{$pass}\n";

demo

Comments

1

After debugging with the post author, I guessed that the problem was with the line return character. Using a \r\n fixed the problem:

$usernames = explode("\n\r", file_get_contents("usernames.txt"));
$passwords = explode("\n\r", file_get_contents("passwords.txt"));

For reference, please note that it is very important not to assume your input data is right. If you see that something is wrong and it points obviously to a mistake you made previously (in that case it is clearly not the foreach function that is buggy, but the array), then you need to swallow your pride and debug your own code. I have been programming PHP for 10 years, and I still have to remember that every single day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.