Skip to content

Instantly share code, notes, and snippets.

@AucT
Last active December 12, 2019 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AucT/55340f911259fd7a6e9a76b766985c6c to your computer and use it in GitHub Desktop.
Save AucT/55340f911259fd7a6e9a76b766985c6c to your computer and use it in GitHub Desktop.
decompact - replace php compact with array
<?php
function decompact(...$params)
{
$minimumParamsForNewLines = 4;
$newLine = "\n";
if (count($params) < $minimumParamsForNewLines) {
$newLine = '';
}
echo "[{$newLine}";
foreach ($params as $i=>$param) {
$comma = ",";
if ($i === count($params) - 1) {
$comma = '';
}
echo "'{$param}' => \${$param}{$comma}{$newLine}";
}
echo "]{$newLine}";
}
decompact('var1', 'var2', 'var3');
decompact('var1', 'var2', 'var3', 'var4');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment