0

I think if I try to research this info more time then I'll find an answer, but I just don't have any energy anymore. Maybe it's silly question, but I hope that you'll try to understand and help without any ridicules. (I've been studying more than 16+ hours some other topics and always missing this one).

Question: I've a file which contains: Email name surname patronymic "\n" Email name surname patronymic "\n" etc

and I just stock on the reading this info on the array... I need an assyc array:

$array = ["email" => $myEmail, "wholeName" => $name+$surname+$patronymic];

Maybe it could be done something in this way:

  foreach (explode("\n", $str) as $pair) {
            list($key, $value) = explode(' ', $pair);
            $final[] = ['email'=>$key, 'value'=>$value];
    }

But this code will generate only this array:

$final[] = ['email'=>$email, 'value'=>$name];

But I need surname and patronymic as well too. Maybe someone from you know the good way for this? It would help me a lot.

One more time: I think I'll find an answer, but I'm really tired and go to sleep and just want to wake up and find an answer here. Thanks in advance.

1 Answers1

0

I think you're already good to go with your existing functionality. Make a try to list the remaining variables called surname and patroymic like this https://eval.in/881233

$str = 'Email name surname patronymic
Email name surname patronymic';

foreach (explode("\n", $str) as $pair) {
            list($key, $value, $surname, $patroymic) = explode(' ', $pair);
            $final[] = ['email'=>$key, 'value'=>$value,'surname'=>$surname, 'patroymic'=>$patroymic];
    }
print '<pre>';
print_r($final);
print '</pre>';
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • if you read the file with `file()` you an skip the first explode as `file()` creates an array of lines –  Oct 17 '17 at 01:38
  • @rtfm Yes, `file()` will help him to read each line to a new array element. @Andrii Hordynskyi see here for file content reading on each single array element https://stackoverflow.com/questions/6159683/read-each-line-of-txt-file-to-new-array-element – A l w a y s S u n n y Oct 17 '17 at 01:43
  • thank, guys. Forgot about this possibility.. Have a nice day you all :) – Andrii Hordynskyi Oct 17 '17 at 06:28