php foreach多维数组问题
我在 foreach 循环中使用多维数组,但没有得到正确的结果。
数组
$mainarray = array(
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
// more ....
}
foreach 循环
foreach($mainarray as $fieldarray){
foreach($fieldarray as $key => $value){
$body .= $value['email_label'].' - '. $value['field_name'];
}
}
我需要名为 email_label 和 field_name 的键的值,但我没有得到正确的结果
I use a multidimensional array in a foreach loop, but i dont get the right results back.
array
$mainarray = array(
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
array('field_name' => 'xx',
'email_label' => 'xxxx',
'validation_type' => 'xxxxx',
'validation_msg' => 'xxxxxx'),
// more ....
}
foreach loop
foreach($mainarray as $fieldarray){
foreach($fieldarray as $key => $value){
$body .= $value['email_label'].' - '. $value['field_name'];
}
}
i need the value's of the key called email_label and field_name, but i dont get the right results back
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于附加到
$body
的代码访问$value
的索引,因此您的原始代码实际上是为了在三级数组上工作而编写的。如果您的数组的结构与您发布的一样,则不需要内部
foreach
循环。Since your code that appends to
$body
accesses indexes of$value
, your original code was effectively written to work on a three-level array.If your array is structured as you've posted, you don't need the inner
foreach
loop.为此,您只需要一个循环:
You only need one loop for this:
尝试使用
Try to use