PHP: foreach variable assignment and links: how to do it?

I have an array: $ aPerfparse as a 2-dimensional array, where the index is in the range from 0 to n-1,

* aPerfparse[index]['label']         -   label of the perfdata  
*                  ['value']         -   actual perfdata  
*                  ['uom']           -   unit of measurement (might be NULL)

Iterate through each element and set the value of each index and label to the segment. index based variable.

Without a loop, this would be:

$value0 = $aPerfdata[0]['value'];  
$value1 = $aPerfdata[1]['value'];

What is right / wrong in this:

foreach ( $aPerfdata as $key => $value ) {  
    $value$key = $aPerfdata[$key]['value'];  
    $label$key = $aPerfdata[$key]['label'];   
}

Similarly, I need to take these saved variables $ value and $ label and refer to them later in the foreach loop.

Without a loop, it would look like this:

ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+2, $oShadow, $fontFile, $label0 . ":" . " " . $value0);  
ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+40, $oShadow, $fontFile, $label1 . ":" . " " . $value1);

What is right / wrong in this:

foreach ( $aPerfdata as $key => $value ) {  
    ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, $label$key . ":" . " " . $value$key);  
    sz=$sz+40;  
} 

Thank!

====

After each help, I have the following:

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;
}

I no longer need to smooth the array. I tried the method mentioned by Mark, but the ImageTTFText function is not executed.

+3
4

foreach ( $aPerfdata as $value ) {  
    ImageTTFText($img, $fontSize, $fontRotation, 2, $fontSize+$sz, $oShadow, $fontFile, $value['label'] . ":" . " " . $value['value']);  
    $sz=$sz+40;  
} 

, , . - :

${'value'.$key}

, , , ( ).

0

-:

$label$key

. :

$label.$key

(.).

. var_dump , ?

+1

, foreach .

, :

Array
(
    [0] => Array
        (
            [label] => red
            [value] => 8
            [uom] => cm
        )

    [1] => Array
        (
            [label] => green
            [value] => 6
            [uom] => m
        )

    [2] => Array
        (
            [label] => blue
            [value] => 34
            [uom] => m
        )

)

/ .


foreach($arr as $array_key => $array_value)
{
        // obviously you don't need to assign these. it just for demonstration
        $key = $array_key;
        $label = $array_value['label'];
        $value = $array_value['value'];
        $uom = $array_value['uom'];
}

, .

+1

. foreach . :

foreach ( $aPerfdata as $item ) {  
    $value = $item['value'];  
    $label = $item['label'];   
}

In addition, I do not think that you should include a $variable in your name, except, of course, before the name.

0
source

Source: https://habr.com/ru/post/1765738/


All Articles