PHP: foreach variable assignment and referencing: how-to?

palapalaxx

New Member
I have an array:$aPerfparse as 2-dimensional array where index ranges from 0 to n-1, \[code\]* aPerfparse[index]['label'] - label of the perfdata * ['value'] - actual perfdata * ['uom'] - unit of measurement (might be NULL)\[/code\]Need to iterate through each item and set each indexes 'value' and 'label' to a sep. variable based-on the index. Without a loop, it would be: \[code\]$value0 = $aPerfdata[0]['value']; $value1 = $aPerfdata[1]['value'];\[/code\]What is correct/incorrect about this?: \[code\]foreach ( $aPerfdata as $key => $value ) { $value$key = $aPerfdata[$key]['value']; $label$key = $aPerfdata[$key]['label']; }\[/code\]Similarly, I need to take those stored $value and $label variables and reference them later in a foreach loop. Without a loop, it would look like: \[code\]ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+2, $oShadow, $fontFile, $label0 . ":" . " " . $value0); ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+40, $oShadow, $fontFile, $label1 . ":" . " " . $value1);\[/code\]What is correct/incorrect about this?: \[code\]foreach ( $aPerfdata as $key => $value ) { ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, $label$key . ":" . " " . $value$key); sz=$sz+40; } \[/code\]Thank you! ====After everyone's help, I have the following working: \[code\]foreach ( $aPerfdata as $key => $value ) { ${'label'.$key} = $aPerfdata[$key]['label']; ${'value'.$key} = $aPerfdata[$key]['value']; }foreach ( $aPerfdata as $key => $value ){ ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, ${'label'.$key} . ":" . " " . ${'value'.$key}); $sz=$sz+40;}\[/code\]I don't really have a need to flatten the array anymore. I tried the method mentioned by Mark, but the ImageTTFText function doesn't execute.
 
Back
Top