I am trying to create a multidimensional array in PHP using a foreach loop. Here is the code thus far:
$levels = array('low', 'medium', 'high');
$attributes = array('fat', 'quantity', 'ratio', 'label');
foreach ($levels as $key => $level):
foreach ($attributes as $k =>$attribute):
$variables[] = $attribute . '_' . $level;
endforeach;
endforeach;
echo '<pre>' . print_r($levels,1) . '</pre>';
echo '<pre>' . print_r($variables,1) . '</pre>';
The output from this code is a single dimension array; however, that is not the intent. The desired array should look like this:
How should the code be modified to achieve the goal?
See Question&Answers more detail:os