【问题标题】:Variable in an array, is this possible? syntax error probably数组中的变量,这可能吗?语法错误可能
【发布时间】:2012-07-30 11:59:07
【问题描述】:

首先我通过代码声明一个我想在多个数组中使用的变量。这是它应该工作的sn-p,但它没有:

变量:

$test = '<div id="test"> dit is een test </div>'; 

数组:

$sections[] = array( 
                'title' => $test, 
                'icon' => '/img/icons/home.png' 
                );  

标题始终为空:-/

谢谢!!

【问题讨论】:

  • 你能提供更多你的脚本吗?您提供的代码应该可以工作。
  • 您的代码没有问题。 $sections[0]['title'] 应该可以正常显示(假设它是数组中的第一个元素)
  • 是的,这是可能的,你尝试过 echo $sections[0]['title'];获得标题......这应该给你标题
  • 代码没问题。数组声明可能位于无法读取 $test 的位置(即范围问题)。无论哪种方式,我们都需要查看更多代码。
  • 您是否通过 var_dump($sections) 查看存储在数组中的值??

标签: php arrays variables syntax


【解决方案1】:

正确的声明是

$test = '<div id="test"> dit is een test </div>'; 
$sections = array( 
                'title' => $test, 
                'icon' => '/img/icons/home.png' 
                );  

【讨论】:

  • 感谢大家的快速响应。事实上,tha 代码是来自 wordpress 框架的 sn-p。它是管理面板中的一个菜单项。上面的代码不起作用,我收到一个错误“为 xxx/options.php 中的 foreach() 提供的参数无效”,所以它比我想象的要复杂。
  • 我找到了,问题出在上面代码的第一行:$sections = array();谢谢大家!
【解决方案2】:

试试这个代码:

$test = '<div id="test"> dit is een test </div>';

$sections['title'] = $test;
$sections['icon'] = '/img/icons/home.png';

echo $sections['title'];

【讨论】:

    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 2011-06-14
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2015-07-24
    相关资源
    最近更新 更多