3v4l.org

run code in 300+ PHP versions simultaneously
<?php // generic function to generate cross product of multi-dimensional array function arrayCrossProduct( array $data ) { $rowCount = 1; foreach( $data as &$datum ) { $datum = (array) $datum; $rowCount *= count( $datum ); reset( $datum ); // reset the array, just to be sure } unset( $datum ); // drop the reference, just to be safe $result = array(); while( $rowCount-- > 0 ) { $row = array(); foreach( $data as &$datum ) { $row[] = current( $datum ); } $result[] = $row; foreach( $data as &$datum ) { next( $datum ); if( !is_null( key( $datum ) ) ) { break; } reset( $datum ); } } return $result; } // example data $data = array( 'name' => 'Mike', 'country' => 'UK', 'prefers' => array( 'coffee', 'milk', 'bananas' ), 'age' => 25, 'parents' => array( 'John', 'Janine' ), 'children' => array( 'Jack', 'Jill', 'Joline' ) ); $i = 1; foreach( arrayCrossProduct( $data ) as $row ) { echo str_pad( (string) $i++, 2, " ", STR_PAD_LEFT ) . '. ' . implode( ', ', $row ) . PHP_EOL; }
Output for git.master, git.master_jit, rfc.property-hooks
1. Mike, UK, coffee, 25, John, Jack 2. Mike, UK, milk, 25, John, Jack 3. Mike, UK, bananas, 25, John, Jack 4. Mike, UK, coffee, 25, Janine, Jack 5. Mike, UK, milk, 25, Janine, Jack 6. Mike, UK, bananas, 25, Janine, Jack 7. Mike, UK, coffee, 25, John, Jill 8. Mike, UK, milk, 25, John, Jill 9. Mike, UK, bananas, 25, John, Jill 10. Mike, UK, coffee, 25, Janine, Jill 11. Mike, UK, milk, 25, Janine, Jill 12. Mike, UK, bananas, 25, Janine, Jill 13. Mike, UK, coffee, 25, John, Joline 14. Mike, UK, milk, 25, John, Joline 15. Mike, UK, bananas, 25, John, Joline 16. Mike, UK, coffee, 25, Janine, Joline 17. Mike, UK, milk, 25, Janine, Joline 18. Mike, UK, bananas, 25, Janine, Joline

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
76.3 ms | 402 KiB | 8 Q