새 구문으로 PHP 배열을 변환하십시오
-
21-12-2019 - |
문제
나는 이전의 배열을 sublimetext로 새로운 php 구문으로 변환하는 방법 (정규식, 스 니펫, 플러그인 등)을 찾고 있습니다.
// Old synthax
$var = array(
'foo' => 'bar'
);
// New synthax
$var = [
'foo' => 'bar'
];
.
누군가가 아이디어를 가지고 있습니까?
해결책
나는 직업을 완벽하게하는 스크립트를 발견했습니다!
다른 팁
PHP Codesniffer 로이 작업을 수행 할 수도 있습니다. https://github.com/ squizlabs / php_codesniffer
phpcbf src/ --standard=Generic --sniffs=Generic.Arrays.DisallowLongArraySyntax
.
이 예제에서는 src / 스크립트가 들어있는 폴더와 함께 바꾸어야합니다.또는 파일 이름을 제공 할 수 있습니다.
어쩌면 조금 늦었지만 나는 내 자신을 만들었습니다.어쩌면 예쁘지 않을 수도 있지만, 내가 원하는 직업은 그렇게합니다.탭을 좋아하지 않으면, 들여 쓰기 함수에서 \ t를 2 또는 4 공백으로 변경하십시오.
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;
}
. 제휴하지 않습니다 StackOverflow