3v4l.org

run code in 300+ PHP versions simultaneously
<?php function cartesian($input) { $result = array(); while (list($key, $values) = each($input)) { if (empty($values)) { continue; } if (empty($result)) { foreach($values as $value) { $result[] = array($key => $value); } } else { $append = array(); foreach($result as &$product) { $product[$key] = array_shift($values); $copy = $product; foreach($values as $item) { $copy[$key] = $item; $append[] = $copy; } array_unshift($values, $product[$key]); } $result = array_merge($result, $append); } } return $result; } $data = array ( 'machineAttribute' => array ( 'TTT 1000 S', 'TTT 1100 S' ), 'technicalAttribute' => array ( 'Certificate', 'Software' ), 'languageAttribute' => array ( 'English', 'Spanish', ) ); $combos = cartesian($data); $final_result = []; foreach($combos as $combo){ $final_result[] = implode('/',$combo); } print_r($final_result);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Fatal error: Uncaught Error: Call to undefined function each() in /in/Zh6Ws:5 Stack trace: #0 /in/Zh6Ws(58): cartesian(Array) #1 {main} thrown in /in/Zh6Ws on line 5
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/Zh6Ws on line 5 Array ( [0] => TTT 1000 S/Certificate/English [1] => TTT 1100 S/Certificate/English [2] => TTT 1000 S/Software/English [3] => TTT 1100 S/Software/English [4] => TTT 1000 S/Certificate/Spanish [5] => TTT 1100 S/Certificate/Spanish [6] => TTT 1000 S/Software/Spanish [7] => TTT 1100 S/Software/Spanish )
Output for 7.1.25, 7.3.32 - 7.3.33
Array ( [0] => TTT 1000 S/Certificate/English [1] => TTT 1100 S/Certificate/English [2] => TTT 1000 S/Software/English [3] => TTT 1100 S/Software/English [4] => TTT 1000 S/Certificate/Spanish [5] => TTT 1100 S/Certificate/Spanish [6] => TTT 1000 S/Software/Spanish [7] => TTT 1100 S/Software/Spanish )

preferences:
134.41 ms | 410 KiB | 5 Q