8

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:

OutputGoal

How should the code be modified to achieve the goal?

1
  • Are you trying to combine two arrays into a single array, or create a new array from the data in those two arrays? Commented Aug 8, 2013 at 20:42

3 Answers 3

16

You're aaalmost there. Just add the level to the array creation :)

$levels = array('low', 'medium', 'high');
$attributes = array('fat', 'quantity', 'ratio', 'label');

foreach ($levels as $key => $level):
       foreach ($attributes as $k =>$attribute):
             $variables[$level][] = $attribute . '_' . $level; // changed $variables[] to $variables[$level][]
       endforeach;
endforeach;

echo '<pre>' . print_r($levels,1) . '</pre>';   
echo '<pre>' . print_r($variables,1) . '</pre>';  

Output

Array
(
    [low] => Array
        (
            [0] => fat_low
            [1] => quantity_low
            [2] => ratio_low
            [3] => label_low
        )

    [medium] => Array
        (
            [0] => fat_medium
            [1] => quantity_medium
            [2] => ratio_medium
            [3] => label_medium
        )

    [high] => Array
        (
            [0] => fat_high
            [1] => quantity_high
            [2] => ratio_high
            [3] => label_high
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

I have this problem. I need someting like that out of a foreach. $arrOptions = array( array( 'value' => 'value1', 'label' => 'label1' ) ); Foreach value and label is the same variable. What i try is $arrOptions = array(); foreach($daterange as $option) { $arrOptions[value] = $option->format; $arrOptions[label] = $option->format; }
6
$levels = ['low', 'medium', 'high'];
$attributes = ['fat', 'quantity', 'ratio', 'label'];

$result = [];
foreach ($levels as $level) {
    $result[$level] = [];
    foreach ($attributes as $attribute) {
        $result[$level][] = $attribute . '_' . $level;
    }
}

var_dump($result);

4 Comments

If you are editing, this code-only answer is missing its educational explanation.
I reformatted code in my own seven-years-old answer. I don't have anything to add. :)
You could explain how the OP's code was failing and/or how your code manages to produce the expected output structure.
@TomaszKowalczyk thank you for supplying this answer; it got me out of a coding nadir converting a multi-dimensional array to CSV.
1
$levels = ['low', 'medium', 'high'];
$attributes = ['fat', 'quantity', 'ratio', 'label'];

foreach ($levels as $level) {
    foreach ($attributes as $attribute) {
        $variables[$level][] = $attribute . '_' . $level;
   }
}

print_r($variables);

http://codepad.viper-7.com/xlvZ2W

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.