[英]php foreach loop not working correctly
public function include_header( $h, $h_data ) {
// Require header file
if (isset($h) && !empty($h)) {
$this->header_file = 'includes/header/'.$h;
} else { return false; }
// Pass optional array of parameters
if (isset($h_data) && is_array($h_data) && !empty($h_data)) {
// Loop through array & assign keys to appropriate class members
foreach($h_data as $key => $val) {
if ($key == 'doctype') { $this->doctype = $val; }
if ($key == 'title') { $this->title = $val; }
if ($key == 'meta') {
// The meta key is should be an array in h_data
// so, we'll have to loop through the meta array
// in order to parse the individual meta elements.
foreach($key as $meta_key => $meta_val) {
$this->error = $meta_key;
}
}
}
} else { return false; }
}
我正在尝试遍历多维数组,其中有以下变量...
$ h_data ['meta'] ['individual_element']
我试图遍历“元”,以便可以访问每个单独的值,但是遇到了麻烦。 请帮忙! 提前致谢!
foreach($key as $meta_key => $meta_val) {
$this->error = $meta_key;
}
... 应该
foreach($val as $meta_key => $meta_val) {
$this->error = $meta_key;
}
if ($key == 'meta') {
// The meta key is should be an array in h_data
// so, we'll have to loop through the meta array
// in order to parse the individual meta elements.
foreach($val as $meta_key => $meta_val) { //val not key
$this->error = $meta_key;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.