Converti array PHP con la nuova sintassi
-
21-12-2019 - |
Domanda
Sto cercando un modo (regex, snippet, plugin ecc.) Per convertire i vecchi array con la nuova sintassi PHP con sublimet.
// Old synthax
$var = array(
'foo' => 'bar'
);
// New synthax
$var = [
'foo' => 'bar'
];
.
qualcuno ha un'idea?
Soluzione
Ho trovato uno script che fa il lavoro perfettamente!
https://github.com/thomasbachem/php-short-array-Syntax-Converter
Altri suggerimenti
Ho scoperto che è anche possibile farlo con il codice PHPNiffer: https://github.com/ SquizLabs / Php_codesniffer
phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax
.
In questo esempio è necessario sostituire SRC / con la cartella contenente gli script.In alternativa è possibile fornire un nome di file.
Forse un po 'tardi, ma ho creato il mio.Forse non carino, ma fa il lavoro che voglio.Se non è simile alla scheda, modificare la funzione di rientro su 2 o 4 spazi.
function loopArray(array $array, $loopcount = 0) {
$returnString = ($loopcount == 0) ? "[\n" : "";
$tabKey = indent($loopcount + 2);
$tabValue = indent($loopcount + 3);
$lastKey = array_key_last($array);
foreach ($array as $key => $value) {
$totalChildren = count($array[$key]);
$returnString .= $tabKey . '"' . $key . '" => ';
if ($totalChildren == 0) $returnString .= '[]';
if ($totalChildren > 0 && is_array($array[$key])) $returnString .= '[' . "\n";
if (is_array($value)) {
$returnString .= loopArray($value, $loopcount + 1);
} else {
if ($totalChildren == 1) $returnString .= '"' . $value . '"';
if ($totalChildren > 1) $returnString .= $tabValue . '"' . $value . '"' . ",\n";
}
$returnString .= ($lastKey == $key) ? "\n" . indent($loopcount+1) . "]" : ",\n";
}
return $returnString;
}
function indent($ammount) {
return str_repeat("\t", $ammount);
}
/** use function below only prior to php 7.3 */
function array_key_last(array $array) {
$key = NULL;
if ( is_array( $array ) ) {
end( $array );
$key = key( $array );
}
return $key;
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow