Help us understand the problem. What is going on with this article?

# 1.rangeで配列生成してarray_filterで抽出してからarray_fill_keysで文字を埋めてarray_replaceで合体してarray_mapで要素を出力

fizz_buzz.php
```<?php
\$array = range(0, 100);

\$fizz_array = array_fill_keys(array_filter(\$array, function(\$value) {
return \$value % 3 === 0;
}), 'Fizz');

\$buzz_array = array_fill_keys(array_filter(\$array, function(\$value) {
return \$value % 5 === 0;
}), 'Buzz');

\$fizz_buzz_array = array_fill_keys(array_filter(\$array, function(\$value) {
return \$value % 3 === 0 && \$value % 5 === 0;
}), 'FizzBuzz');

\$result = array_replace(\$array, \$fizz_array, \$buzz_array, \$fizz_buzz_array);
unset(\$result[0]);

array_map(\$result, function(\$value){
echo \$value . '<br>';
});
```

ちなみにarray_filterの代わりに

```range(0, 100, 3); // rangenの第3引数を指定すると要素の増加数をしていできる。
```

でも可。

# 2.クラスを使う

fizzbuzz.php
```<?php
class FizzBuzz {
const FIZZ = 3;
const BUZZ = 5;
public static \$counter = 0;
private \$number;
public function __construct () {
self::\$counter++;
\$this->number = self::\$counter;
}

public function build() {
return self::getFizzBuzz() ? 'FizzBuzz' :
self::getFizz() ?  'Fizz':
self::getBuzz() ? 'Buzz' : \$this->number;
}

private function getFizzBuzz() {
return \$this->number % self::FIZZ === 0 && \$this->number % self::BUZZ === 0;
}

private function getFizz() {
return \$this->number % self::FIZZ === 0;
}

private function getBuzz() {
return \$this->number % self::BUZZ === 0;
}
}

\$array = array_fill(1, 100, 'Inori Minase');

array_walk(\$array, function(&\$value){
\$fizz_buzz_incetance = new FizzBuzz();
\$value = \$fizz_buzz_incetance->build();
});

\$obj = new ArrayObject(\$array);
\$it = \$obj->getIterator();

while (\$it->valid()){
echo \$it->current() . "</br>";
\$it->next();
}
```

FizzBuzzCounterクラスは、インスタンスが生成されるたびにカウンターを1づつ増やしていくよ。

# おまけ

## chain of responsibility

```<?php
class FizzBuzzClient {
public static \$counter = 0;
private \$number;
public function __construct () {
self::\$counter++;
\$this->number = self::\$counter;
}

public function getNumber() {
return \$this->number;
}
}

abstract class Handler {
private \$next;

public function setNext(Handler \$next) {
\$this->next = \$next;
}

public function request (FizzBuzzClient \$client) {
\$this->resolve(\$client) ? \$this->done() : \$this->request(\$client);
}

abstract protected function resolve(FizzBuzzClient \$client);

abstract protected function done (FizzBuzzClient \$client);

}

class NoHandler extends Handler {
protected function resolve (FizzBuzzClient \$client) {
return false;
}

protected function done (FizzBuzzClient \$client) {
return false;
}
}

class FizzBuzzHandler extends Handler {
protected function resolve (FizzBuzzClient \$client) {
return \$client->getNumber % 3 === 0 && \$client->getNumber % 5;
}

protected function done (FizzBuzzClient \$client) {
return 'FizzBuzz';
}
}

class FizzHandler extends Handler {
protected function resolve (FizzBuzzClient \$client) {
return \$client->getNumber % 3 === 0;
}

protected function done (FizzBuzzClient \$client) {
return 'Fizz';
}
}

class BuzzHandler extends Handler {
protected function resolve (FizzBuzzClient \$client) {
return \$client->getNumber % 3 === 0 && \$client->getNumber % 5;
}

protected function done (FizzBuzzClient \$client) {
return 'Buzz';
}
}

class LastHandler extends Handler {
protected function resolve (FizzBuzzClient \$client) {
return true;
}

protected function done (FizzBuzzClient \$client) {
return \$client->getNumber;
}
}

\$array = array_fill(1, 100, 'Inori Minase');

\$no_handler = new NoHandler();
\$fizz_buzz = new FizzBuzzHandler();
\$fizz = new FizzHandler();
\$buzz = new BuzzHandler();
\$last = new LastHandler();

\$no_handler->setNext(\$fizz_buzz)->setNext(\$fizz)->setNext(\$buzz)->setNext(\$last);

foreach (\$array as \$key => \$value) {
\$fizz_buzz_client = new FizzBuzzClient();
\$no_Handler->request(\$fizz_buzz_client);
}
```

ここまで書いたけどメモリ足りなくて断念した。

# おまけ2

「何って...俺はただif文を使っただけだが...?」ｷｮﾄﾝ

```<?php
for (\$i = 1; \$i <= 100; \$i++) {
if (\$i % 3 == 0 && \$i % 5 == 0) {
echo 'FizzBuzz</br>';
} elseif (\$i % 3 == 0) {
echo 'Fizz</br>';
} elseif (\$i % 5 == 0) {
echo 'Buzz</br>';
} else {
echo \$i .'</br>';
}
}
```
Why do not you register as a user and use Qiita more conveniently?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away