PHP:foreach 变量赋值和引用:如何操作?
我有一个数组: $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先:
是错误的。应该是:
您需要用点(.)连接变量。
不明白问题的第二部分。
您能否粘贴数组的 var_dump,以便我们可以更清楚地了解该结构?
Firstly:
is wrong. Should be:
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?
在我看来,您可能会将 foreach 与简单的 for 循环混淆。
如果您有一个如下所示的数组:
您可以按如下方式访问键/值的迭代。
希望有帮助。
It looks to me like you might be confusing foreach with a simple for loop.
If you have an array that looks like this:
You can access iterate over the keys/values as follows.
Hope that helps.
为什么不这样做
除其他外,我认为您对变量变量的使用是不正确的。你真的应该有这样的东西:
但实际上你应该只使用数组而不是可变变量 - 并且由于你已经有一个数组,所以没有真正需要将其展平(正如我上面向你展示的那样)。
Why not just do
Among other things I think your use of variable variables is incorrect. You should really have something like:
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).
你有一个二维数组。
foreach
语句会为您迭代第一个维度。这应该有效:另外,我认为您不应该在变量名称中包含
$
,当然除了名称前面。You have a two dimensional array. The
foreach
statements iterates through the first dimension for you. This should work:Also, I don't think you should include
$
in your variable name, other than of course in front of the name.