How to use a PHP here-doc in an associative array?

With PHP 7.3 things have improved significantly. You can now do this:

$data = [
  "x" => "y",
  "foo" => "bar",
  /* ... other values ... */
  "idx" => <<<EOC
    data data data data
    data data data data
    data data data data
    EOC,
  "z" => 9,
  /* ... more values ... */
];

I had the same problem and I ended up doing this (old solution):

$data = [
  "x" => "y",
  "foo" => "bar",
  /* ... other values ... */
  "z" => 9,
  /* ... more values ... */
];

$data["idx"] = <<<EOC
data data data data
data data data data
data data data data
EOC;

The idea is that I can use heredoc without extremely ugly array formatting.


There are several problems, it has to look like this:

$data = [
  "x" => "y",
  "foo" => "bar",
  /* ... other values ... */
  // you need to use '=>' 
  "idx" => <<<EOC
data data data data
data data data data
data data data data
EOC
 ,"z" => 9, // you can't end it with a semicolon, WHY EVER! and the comma needs to be on a new line
 /* ... more values ... */
];

That's some hacky and clunky PHP code. I don't recommend to use it, it's full of problems (maybe caused by the lexer). Better stick to good old strings.