patchranger / cartesian-iterator
笛卡尔迭代器
0.07
2019-03-22 17:01 UTC
Requires
- php: >=7.0
README
迭代器,返回关联数组迭代器的笛卡尔积。参见 https://en.wikipedia.org/wiki/Cartesian_product 。
其他实现
- https://github.com/phpbench/phpbench/blob/master/lib/Benchmark/CartesianParameterIterator.php :稍微有些杂乱。
- https://github.com/bpolaszek/cartesian-product :虽然简洁,但经过测试。
- https://stackoverflow.com/a/15973172 :相当简单,虽然是数组而非迭代器。
基准测试
https://github.com/PatchRanger/php-cartesian-benchmark
快速开始
<?php use PatchRanger\CartesianIterator; require 'vendor/autoload.php'; $cartesianIterator = new CartesianIterator(); // The second argument controls the key of the corresponding value in the product array. $cartesianIterator->attachIterator(new ArrayIterator([1,2]), 'test'); // No second argument means incremental numeration (indexed). $cartesianIterator->attachIterator(new ArrayIterator(['foo', 'bar'])); $result = iterator_to_array($cartesianIterator, false); print_r($result);
结果
Array
(
[0] => Array
(
[test] => 1
[1] => foo
)
[1] => Array
(
[test] => 2
[1] => foo
)
[2] => Array
(
[test] => 1
[1] => bar
)
[3] => Array
(
[test] => 2
[1] => bar
)
)