I'm trying to cross join arrays to automatically create data for various test scenarios in an app. Here are the arrays to be cross joined (note that $base['sales']
has had some elements stripped out of it to simplify it for posting this question):
$base['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
$scenarioIDs = range(1, 5);
The end result is supposed to look like this:
$data[1]['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
$data[2]['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
$data[3]['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
$data[4]['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
$data[5]['sales'] = ['shirts'=>1200.0, 'pants'=>1000.0, 'socks'=>1700.0];
I have tried this:
$data = [];
array_map(function($scenarioID) {
$data[$scenarioID]['sales'] = $base['sales'];
}, $scenarioIDs);
echo '<pre>' . print_r($data, 1) . '</pre>';
exit();
but get the message Undefined variable: base in C:\xampp\htdocs\Sales\index.php on line 72
five times (one for each scenarioID).
I keep thinking there must be a way to do this without using loops. Does anyone know how?