PHP的 - 提取数组的全局变量 [英] php - extract array into global variables

查看:171
本文介绍了PHP的 - 提取数组的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上提取表明您可以提取像一个数组的手册:

 提取物(阵列(一=大于1,两节=> 2));

在一个$,$两个... ...

但提取功能不返回的变量。是否有全球化这些变量的方法吗?也许没有使用提取物,但foreach循环?

编辑:(约我想要实现的解释)
我有包含数百个输出消息,我希望有尽可能有效的变量访问数组。我的意思是,每当我要输出一个消息,说:

  $ englishMessages =阵列('的helloWorld'=>的'Hello World');
$ spanishMessages =阵列('的helloWorld'=>世界报霍拉');
'<跨度ID =一些>'。 $为HelloWorld。'< / SPAN>';

将出现该消息。我做的是这样的原因是,使用户可以更改像他们在浏览网页的语言,这样的东西:
    ''。 $的helloWorld'。
会产生:

 霍拉世界报!


解决方案

不完全回答你的问题......但是:保持数组,不污染(全局)变量命名

  $ englishMessages =阵列('的helloWorld'=>的'Hello World');
$ spanishMessages =阵列('的helloWorld'=>世界报霍拉');//在一个不错的功能/法这个包装
$ LANG = $ englishMessages;
//然后用$ LANG用于输出
'<跨度ID =一些>'。 $ LANG ['的helloWorld']。'< / SPAN>';

在同一主题的一些变化:

 函数的getMessages($语言){
  静态$ L =阵列(
    '恩'=>阵列('的helloWorld'=>的'Hello World),
    'ES'=>阵列('的helloWorld'=>世界报霍拉')
  );
  //< - 增加在这里的处理报告 - >
  返回$ L [$语言]
}$ LANG =的getMessages('恩');
呼应'<跨度ID =一些>'。 $ LANG ['的helloWorld']。'< / SPAN>';

 函数__($语言的$ id){
  静态$ L =阵列(
    '恩'=>阵列('的helloWorld'=>的'Hello World),
    'ES'=>阵列('的helloWorld'=>世界报霍拉')
  );
  //< - 添加的错误处理 - >
  返回$ L [$语言] [$ ID];
}呼应'<跨度ID =一些>'。 __('上课','的helloWorld')'< / SPAN>'。

您可能也有兴趣在 http://docs.php.net/gettext

The manual on "extract" shows you can extract an array like:

extract(array('one'=>1,'two'=>2));

into $one,$two...

But the extract function doesn't return the variables. Is there a way to 'globalize' these variables? Maybe not using extract, but a foreach loop?

EDIT: (explanation about what I'm trying to achieve) I have an array containing hundreds of output messages which I want to have accessible as variables efficiently. What I mean is that whenever I want to output a message, say:

$englishMessages = array('helloWorld'=>'Hello World');
$spanishMessages = array('helloWorld'=>'Hola Mundo');
'<span id="some">'. $helloWorld .'</span>';

The message would appear. The reason I'm doing it like this is so that users can change the language they're viewing the website in, so something like: ''. $helloWorld .''; would produce:

Hola Mundo!

解决方案

Not exactly an answer to your question ...but: Keep the array, don't pollute the (global) variable namespace.

$englishMessages = array('helloWorld'=>'Hello World');
$spanishMessages = array('helloWorld'=>'Hola Mundo');

// wrap this in a nice function/method
$lang = $englishMessages;
// then use $lang for the output
'<span id="some">'. $lang['helloWorld'] .'</span>';

Some variations on the same theme:

function getMessages($language) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  );
  // <-- add handling reporting here -->
  return $l[$language];
}

$lang = getMessages('en');
echo '<span id="some">'. $lang['helloWorld'] .'</span>';

or

function __($language, $id) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  );
  // <-- add error handling here -->
  return $l[$language][$id];
}

echo '<span id="some">'. __('es', 'helloWorld') .'</span>';

You might also be interested in http://docs.php.net/gettext

这篇关于PHP的 - 提取数组的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆