5
1

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 5 years have passed since last update.

PHP: splatオペレータには整数添字配列のキーを振り直す効果がある

5
Posted at
function x(int ...$numbers)
{
    return $numbers;
}

assert(x(...[1, 2, 3]) === [1, 2, 3]);

// 添字配列の添字は消失し、ゼロからの添字に振り直される
assert(x(...[100 => 1, 2, 3]) === [0 => 1, 2, 3]);

try {
    // 連想配列はFatal Errorになる。
    x(...['a' => 1, 'b' => 2, 'c' => 3]);
    assert(false, '例外が発生するから、ここには来ない');
} catch (Error $e) {
    assert('Cannot unpack array with string keys' === $e->getMessage());
}
5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?