LoginSignup
1
0

More than 5 years have passed since last update.

参考になったコードを貼り付けるだけ【PHP】

Last updated at Posted at 2018-05-31

array_map

引数のidが配列だった場合は、array_mapのコールバックでadd()を行う。

Cart::add('293ad', 'Product 1', 1, 9.99);
Cart::add([
  ['id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00],
  ['id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => ['size' => 'large']]
]);

public function add($id, $name = null, $qty = null, $price = null, array \$options = [])
{
    if ($this->isMulti($id)) {
        return array_map(function ($item) {
            return $this->add($item);
        }, $id);
    }
    ...
}

filter_input

1〜5の整数値でバリデーションを行う。デフォルトで3を設定。

$options = array(
    'options' => array(
        'default'   => 3,
        'min_range' => 1,
        'max_range' => 5,
    ),
);
$rating  = filter_input( INPUT_POST, 'score', FILTER_VALIDATE_INT, $options );
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