我有一个遍历并收集数据的数组,但是我正在尝试将该变量作为另一个数组中的键重用,当我回显该变量时,它是一个字符串,但是当我在变量中使用它时它显示为空。
以$ value等于“ about”为例
订单布局数组输出
Array
(
[1] => benefits
[2] => services
[3] => about
[4] => team
[5] => successstories
[6] => cta_banner
[7] => social
[8] =>
[9] =>
[10] =>
[11] =>
)
$ bp_theme_options数组变量
$bp_theme_options['web_sec']['about']['show_row'] = "yes";
$bp_theme_options['web_sec']['affiliates']['show_row'] = "yes";
$bp_theme_options['web_sec']['benefits']['show_row'] = "yes";
$bp_theme_options['web_sec']['blog']['show_row'] = "yes";
$bp_theme_options['web_sec']['services']['show_row'] = "yes";
$bp_theme_options['web_sec']['social']['show_row'] = "yes";
$bp_theme_options['web_sec']['success_stories']['show_row'] = "yes";
$bp_theme_options['web_sec']['team']['show_row'] = "yes";
$bp_theme_options['web_sec']['cta_banner']['show_row'] = "yes";
错误的输出-$ contents_show为NULL
foreach ( $order_layout as $key => $value ) {
if($value){
var_dump($value) //returns 'about' string
$contents_show = $bp_theme_options['web_sec'][$value]['show_row'];
echo $contents_show; //should return 'yes' returns NULL
}
}
正确的输出-$ contents_show为“是”;
$contents_show = $bp_theme_options['web_sec']['about']['show_row'];
$ value返回
string(8) "about"
在另一个数组中使用变量作为键是否存在问题?如果我手动使用“关于”,它会成功获取变量。
答案 0 :(得分:1)
就我而言,它正常工作。
$order_layout = array(0=>'benefits',1=>'services',2=>'about',3=>'team',4=>'success_stories',5=>'cta_banner',6=>'social',7=>'',8=>'',9=>'',10=>'');
$bp_theme_options['web_sec']['about']['show_row'] = "yes";
$bp_theme_options['web_sec']['affiliates']['show_row'] = "yes";
$bp_theme_options['web_sec']['benefits']['show_row'] = "yes";
$bp_theme_options['web_sec']['blog']['show_row'] = "yes";
$bp_theme_options['web_sec']['services']['show_row'] = "yes";
$bp_theme_options['web_sec']['social']['show_row'] = "yes";
$bp_theme_options['web_sec']['success_stories']['show_row'] = "yes";
$bp_theme_options['web_sec']['team']['show_row'] = "yes";
$bp_theme_options['web_sec']['cta_banner']['show_row'] = "yes";
foreach ( $order_layout as $key => $value ) {
if ($value) {
$contents_show = $bp_theme_options['web_sec'][$value]['show_row'];
var_dump($contents_show . "<br>"); //should return 'yes' and returns 'yes'
}
}
输出:
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
string 'yes<br>' (length=7)
您在$ order_layout和$ bp_theme_options中使用不同的名称(请检查successstories和success_stories)。也许您在$ order_layout中的“关于”名称中有多余的空格?
答案 1 :(得分:1)
就我而言,完美地工作。
<?php
$bp_theme_options['web_sec']['about']['show_row'] = "yes";
$bp_theme_options['web_sec']['affiliates']['show_row'] = "yes";
$bp_theme_options['web_sec']['benefits']['show_row'] = "yes";
$bp_theme_options['web_sec']['blog']['show_row'] = "yes";
$bp_theme_options['web_sec']['services']['show_row'] = "yes";
$bp_theme_options['web_sec']['social']['show_row'] = "yes";
$bp_theme_options['web_sec']['success_stories']['show_row'] = "yes";
$bp_theme_options['web_sec']['team']['show_row'] = "yes";
$bp_theme_options['web_sec']['cta_banner']['show_row'] = "yes";
$order_layout = [ 1 =>'benefits', 2 =>'services', 3 =>'about', 4 =>'team', 5 =>'success_stories', 6 =>'cta_banner', 7 =>'social'];
foreach ( $order_layout as $key => $value ) {
if($value){
if(isset($bp_theme_options['web_sec'][$value]['show_row'])){
$contents_show = $bp_theme_options['web_sec'][$value]['show_row'];
if(isset($contents_show)){
var_dump($contents_show.'--'.$value);
}
}
}
}
?>
输出:
string(13) "yes--benefits"
string(13) "yes--services"
string(10) "yes--about"
string(9) "yes--team"
string(20) "yes--success_stories"
string(15) "yes--cta_banner"
string(11) "yes--social"
答案 2 :(得分:0)
是否存储实际循环值的引用?
foreach ( $order_layout as $key => $value ) {
if($value){
var_dump($value) //returns 'about' string
$value_ref = &$value;
$contents_show = $bp_theme_options['web_sec'][$value_ref]['show_row'];
echo $contents_show; //should return 'yes' returns NULL
}
}