1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

配列内スペースを余分にとってソースの見栄えをよくする

Posted at

PHPを使っているとよく配列を使うのですが、keyと要素を揃えて書くのが好きです。

人により好みが分かれるところだと思いますが私の場合以下の二つであれば下の書き方(sample2.php)を好みます。

sample1.php
$input = array
(
    "id" => 1,
    "name" => "sato",
    "postal_code" => "000-0000",
    "address" => "Tokyo",
    "phone_number" => "09012345678"
);
sample2.php
$input = array
(
    "id"           => 1,
    "name"         => "sato",
    "postal_code"  => "000-0000",
    "address"      => "Tokyo",
    "phone_number" => "09012345678"  // 揃っていて見やすい!
);

しかし後から長い要素が増えると全てをいちいち揃えることになってしまい面倒です。
そんな時はあらかじめスペースを余分に取っておくと楽かもしれません。

sample2.php
$input = array
(
    "id"                 => 1,
    "name"               => "sato",
    "postal_code"        => "000-0000",
    "address"            => "Tokyo",
    "phone_number"       => "09012345678"
);

こうしておけば例え長い要素が増えたとしても対応できます。

sample3.php
$input = array
(
    "id"                 => 1,
    "name"               => "sato",
    "postal_code"        => "000-0000",
    "address"            => "Tokyo",
    "phone_number"       => "09012345678",
    "Family structure"   => "single"   // 追加要素
);

大した話ではないですが、他人の書いたソースを見ていて自分なりにテンションが上がったので書いてみました。色々応用が効きそうです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?