fork download
  1. <?php
  2.  
  3. $text = 'Зеленое яблоко
  4. Оранжевый апельсин
  5. Синее море';
  6.  
  7. $strings = explode("\n", $text);
  8.  
  9. $foo = function($c, $str) {
  10. $spacePos = mb_strpos($str, ' ');
  11. $key = mb_substr($str, 0, $spacePos);
  12. $val = mb_substr($str, 1 + $spacePos);
  13. $c[$key] = $val;
  14. return $c;
  15. };
  16.  
  17. $result = array_reduce($strings, $foo, []);
  18.  
  19. var_export($result);
Success #stdin #stdout 0.01s 24448KB
stdin
Standard input is empty
stdout
array (
  'Зеленое' => 'яблоко',
  'Оранжевый' => 'апельсин',
  'Синее' => 'море',
)