PHP:foreach 变量赋值和引用:如何操作?

发布于 2024-09-24 05:49:04 字数 1709 浏览 2 评论 0原文

我有一个数组: $aPerfparse 作为二维数组,其中索引范围从 0 到 n-1,

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

需要迭代每个项目并将每个索引“值”和“标签”设置为 sep。基于索引的变量。

如果没有循环,它将是:

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

对此正确/不正确的是什么?:

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

类似地,我需要获取那些存储的 $value 和 $label 变量,并稍后在 foreach 循环中引用它们。

如果没有循环,它会看起来像:

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

这是正确/错误的是什么?:

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

谢谢!

====

经过大家的帮助,我进行了以下工作:

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

我真的不需要再展平数组了。我尝试了Mark提到的方法,但是ImageTTFText函数没有执行。

I have an array:
$aPerfparse as 2-dimensional array where index ranges from 0 to n-1,

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

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:

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

What is correct/incorrect about this?:

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

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:

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

What is correct/incorrect about this?:

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

Thank you!

====

After everyone's help, I have the following working:

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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

变身佩奇 2024-10-01 05:49:04

首先:

$label$key

是错误的。应该是:

$label.$key

您需要用点(.)连接变量。

不明白问题的第二部分。
您能否粘贴数组的 var_dump,以便我们可以更清楚地了解该结构?

Firstly:

$label$key

is wrong. Should be:

$label.$key

You need to concatenate the variables with a dot(.).

Didn't understand the second part of the question.
Can you just paste a var_dump of the array so we can get a clearer understanding of the structure?

鸠书 2024-10-01 05:49:04

在我看来,您可能会将 foreach 与简单的 for 循环混淆。

如果您有一个如下所示的数组:

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's just for demonstration
        $key = $array_key;
        $label = $array_value['label'];
        $value = $array_value['value'];
        $uom = $array_value['uom'];
}

希望有帮助。

It looks to me like you might be confusing foreach with a simple for loop.

If you have an array that looks like this:

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
        )

)

You can access iterate over the keys/values as follows.


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

Hope that helps.

梨涡少年 2024-10-01 05:49:04

为什么不这样做

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

除其他外,我认为您对变量变量的使用是不正确的。你真的应该有这样的东西:

${'value'.$key}

但实际上你应该只使用数组而不是可变变量 - 并且由于你已经有一个数组,所以没有真正需要将其展平(正如我上面向你展示的那样)。

Why not just do

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

Among other things I think your use of variable variables is incorrect. You should really have something like:

${'value'.$key}

But really you should just be using an array rather than variable variables - and since you already have an array there's no real need to flatten it (as I show you above).

国产ˉ祖宗 2024-10-01 05:49:04

你有一个二维数组。 foreach 语句会为您迭代第一个维度。这应该有效:

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

另外,我认为您不应该在变量名称中包含 $ ,当然除了名称前面。

You have a two dimensional array. The foreach statements iterates through the first dimension for you. This should work:

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

Also, I don't think you should include $ in your variable name, other than of course in front of the name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文