Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:数组声明中的变量_Php_Debugging_Syntax_Variable Assignment - Fatal编程技术网

PHP:数组声明中的变量

PHP:数组声明中的变量,php,debugging,syntax,variable-assignment,Php,Debugging,Syntax,Variable Assignment,我在PHP中工作,从foreach{}循环中调用以下函数。此函数需要接受$subTheme作为选项参数,因为其目的是避免不必要的代码重复(DRY,对吧?) 首先,这里是函数: /* * CSS needed to apply the selected styles to text elements. */ function this_site_text_css( $subTheme = '' ) { $GLOBALS['this_theme_mod']; // this is in

我在PHP中工作,从
foreach{}
循环中调用以下函数。此函数需要接受
$subTheme
作为选项参数,因为其目的是避免不必要的代码重复(DRY,对吧?)

首先,这里是函数:

/*
 * CSS needed to apply the selected styles to text elements.
 */
function this_site_text_css( $subTheme = '' ) {

    $GLOBALS['this_theme_mod']; // this is in global scope already

    $themeVarSuffix = $subTheme['var_suffix'];
    $themeClassName = $subTheme['class_name'];

    $content_bg_color  = $this_theme_mod['content_bg_color' . $themeVarSuffix ];
    $page_bg_color     = $this_theme_mod['page_bg_color' . $themeVarSuffix ];
    $primary_color     = $this_theme_mod['primary_theme_color' . $themeVarSuffix ];

    $css = 'body' . $themeClassName . '{ /* special classes */ };'

    return $css
}
有更多的事情发生,但它相当乏味,只是连接CSS作为一个字符串返回

它的名字是这样的

$data = '';
$data .= this_site_text_css();
$subThemeArray = array(
  'theme_a' => array( 
     'var_suffix' => '_theme_a',
     'class_name' => '.theme-a',
  ),
  'theme_b' => array( 
     'var_suffix' => '_theme_b',
     'class_name' => '.theme-b',
  ),
);
foreach( $subThemeArray as $key => $theme ) {
   $data .= this_site_text_css( $theme );
}
我得到了一个PHP警告
非法字符串偏移量'class_name'
,我猜这是因为PHP不希望我在
$themeClassName
声明时串联
$ThemeMarSuffix
内联。我很确定有办法做到这一点,我已经搜索了所有地方,也许我没有搜索到正确的关键字,但任何帮助都将不胜感激

非法字符串偏移量“class\u name”

。。。表示
$subTheme
实际上是一个
字符串
,而不是
数组
。发生这种情况的原因是,在函数声明
$subTheme='
中有一个默认值,并且在调用该值时没有传递该值,如下所示:

$data .= this_site_text_css();
因此,
$subTheme
是一个空字符串,当然没有索引“class\u name”

非法字符串偏移量“class\u name”

。。。表示
$subTheme
实际上是一个
字符串
,而不是
数组
。发生这种情况的原因是,在函数声明
$subTheme='
中有一个默认值,并且在调用该值时没有传递该值,如下所示:

$data .= this_site_text_css();

所以
$subTheme
是一个空字符串,当然没有索引“class\u name”。

$subThemeArray
数组中,索引
主题类应称为
class\u name
$subThemeArray
数组中,索引
主题类应称为
class\u name
,谢谢!就这样。。。只需执行
功能,此功能\u站点\u文本\u css($subTheme=array())
谢谢!就这样。。。只需执行
功能即可实现此功能\u站点\u文本\u css($subTheme=array())
+1个好主意!对于其他人,请注意,这个问题是在回答这个问题之后更新的。我很欣赏这个问题,但我只是在写一些伪代码,所以我最初发布的错误代码与我的工作代码不同,工作代码从来没有错过,只有pseudo code.np,问题是,当发布代码进行调试时,人们永远无法确定代码中什么是打字错误,什么是真正的bug。不管怎样,很高兴@hek2mgl为您找到了答案+1个好球!对于其他人,请注意,这个问题是在回答这个问题之后更新的。我很欣赏这个问题,但我只是在写一些伪代码,所以我最初发布的错误代码与我的工作代码不同,工作代码从来没有错过,只有pseudo code.np,问题是,当发布代码进行调试时,人们永远无法确定代码中什么是打字错误,什么是真正的bug。不管怎样,很高兴@hek2mgl帮你弄明白了