function func($a, $b, ...$params)
{
   var_dump($a, $b, $params);
}

func(1,2,3,4,5);
// int(1)
// int(2)
// array(3) {
//   [0]=>
//   int(3)
//  [1]=>
//  int(4)
//  [2]=>
//  int(5)
// }
// круто!

$a = [1,2,3,4,5];

func(...$a);
// int(1)
// int(2)
// array(3) {
//   [0]=>
//   int(3)
//  [1]=>
//  int(4)
//  [2]=>
//  int(5)
// }
// еще круче!

$a['qq'] = 'qq';

func(...$a);
// PHP Catchable fatal error:
// Cannot unpack array with string keys in /tmp/test.php on line 16
// ну ок

$b = [];
$b[1] = 1;
$b[4] = 4;
$b[2] = 2;
$b[5] = 5;
$b[3] = 3;

func(...$b);
// int(1)
// int(4)
// array(3) {
//   [0]=>
//   int(2)
//   [1]=>
//   int(5)
//   [2]=>
//   int(3)
// }
// WAT?

4 thoughts on “PHP 5.6 — variadic func / splat operator

  1. Массивы в ПХП такие массивы :). “An array in PHP is actually an ordered map. A map is a type that associates values to keys.”

  2. hmm… where did my previous comment go? Well, lets try again:
    Массивы в ПХП такие массивы :)
    “An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.