如何在一行中编写此表达式,codeigniter

时间:2012-05-07 10:33:40

标签: php codeigniter

我有一段代码,我希望在线获得结果

    $lang = $this->config->item('email');
    echo $lang['support_email'] ; die;

当我var_dump $lang结果时

array(11) {
  ["useragent"]=>
  string(11) "CodeIgniter"
  ["mailpath"]=>
  string(17) "/usr/bin/sendmail"
  ["protocol"]=>
  string(4) "mail"
  ["smtp_host"]=>
  string(9) "localhost"
  ["smtp_user"]=>
  string(0) ""
  ["smtp_pass"]=>
  string(0) ""
  ["smtp_port"]=>
  string(2) "25"
  ["system_email"]=>
  string(21) "noreply@elephanti.com"
  ["help_email"]=>
  string(18) "help@elephanti.com"
  ["inquiries_email"]=>
  string(23) "inquiries@elephanti.com"
  ["support_email"]=>
  string(21) "support@elephanti.com"
}

我试过

echo $this->config->item('email')['support_email']

echo echo `$this->config->item('email')->support_email 

请帮助.............

4 个答案:

答案 0 :(得分:3)

$lang = $this->config->item('email', 'support_email');

来自doc:

http://codeigniter.com/user_guide/libraries/config.html

  

// Retrieve a config item named site_name contained within the blog_settings array`

     

$site_name = $this->config->item('site_name',   'blog_settings');

答案 1 :(得分:1)

您只能在PHP 5.4 +

中执行echo $this->config->item('email')['support_email']

否则你能做的最好的事情是:

$lang=$this->config->item('email');echo $lang['support_email'];exit;

或编写自定义功能为您完成。

然后问问自己,为什么你必须在一条线上做这个......?

答案 2 :(得分:1)

  

echo $this->config->item('email')['support_email']

这实际上适用于PHP> 5.4。在旧版本中,不可能在一个语句中执行它,因此您必须将该数组存储在单独的局部变量中。您可以创建一个函数来检索如下的值:

 <?php
 function array_get( array $array, $index ) {
    return isset( $array[$index] ) ? $array[$index] : false;
 }

 echo array_get( $this->config->item( 'email' ), 'support_email' );

但那真的没用。你想在一行中做任何特殊原因吗?

答案 3 :(得分:0)

尝试:

foreach($lang as $key=>$val)
{
  if($key == "support_email")
  {
    echo $val;
  }
}