bpolaszek / cartesian-product Goto Github PK
View Code? Open in Web Editor NEWPHP - A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.
License: MIT License
PHP - A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.
License: MIT License
There's a bug preventing to unroll the same cartesian product object twice:
use function BenTools\CartesianProduct\cartesian_product;
$data = [
'hair' => [
'blond',
'black'
],
'eyes' => [
'blue',
'green',
function (array $combination) { // You can use closures to dynamically generate possibilities
if ('black' === $combination['hair']) { // Then you have access to the current combination being built
return 'brown';
}
return 'grey';
}
]
];
$combinations = cartesian_product($data);
foreach ($combinations as $combination) {
printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
}
foreach ($combinations as $combination) {
printf('Hair: %s - Eyes: %s' . PHP_EOL, $combination['hair'], $combination['eyes']);
}
Produces:
Hair: blond - Eyes: blue
Hair: blond - Eyes: green
Hair: blond - Eyes: grey
Hair: black - Eyes: blue
Hair: black - Eyes: green
Hair: black - Eyes: brown
PHP Notice: Undefined index: eyes
Notice: Undefined index: eyes
Hair: blond - Eyes:
PHP Notice: Undefined index: eyes
Notice: Undefined index: eyes
Hair: black - Eyes:
I've created a benchmark in order to compare my own cartesian iterator implementation with others, including yours: https://github.com/PatchRanger/php-cartesian-benchmark . It looks like your library is the best in all cases except the last synthetic one. I believe you may want to add the link to the benchmark to README in order to convince to use your library. Up to you.
round(memory_get_usage() / 1024 / 1024),
round(memory_get_peak_usage() / 1024 / 1024)
memory_get_usage()
capture before the cartesian function is called should be subtracted from these results to not pollute it with autoloading and what not which is done before that.
Also these functions are the best for getting a memory profile. Use xdebug profiler for good memory usage information. But don't do benchmarking and profiling at the same time because profiling slows down code.
0 MB / 1 MB could be more accurate, maybe use bigger test input
Hey, I've run into your useful library and as far as I see it's the only one that does cartesian products while preserving string keys. However, it does not support Iterator as subsets, because of this exception being thrown
My use case is to do cartesian product of two mongodb cursors.
Hello,
Can you library do this?
$data = [
'hair' => [
'blond',
'red',
],
'eyes' => [
'blue',
'green',
'brown',
],
'gender' => [
'male',
'female',
]
];
Generate all combinations based on pick any 1 item from hair, pick any 2 items from eyes and any 1 item from gender. And where it can be any number of item arrays and any number of items in the item array, and I can pass as second option how many items to choose from from each item array.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.