range — ある範囲の整数を有する配列を作成する

以前ハノイの塔のアルゴリズムを書いた時に、1~xまでの整数値を持つ配列を作成していましたが、

[php]
for ($i = 1; $i <= $a; $i++) {
$this->a[] = $i;
}
[/php]
こんな恥ずかしいことをしていましたが、rangeで一発ですね。
[php]
$this->a = range(1,$a);
[/php]
いやー、この手の関数があるとは思っていたんですが、Array~うんちゃらで探してて見つからなかったので諦めてました。

関連する記事:

Powered by

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">