PHP array() 的缩写
我不知道你怎么想,但我不太喜欢 PHP 中构造数组的方式。我有这样的感觉,我太频繁地使用 array
关键字,并且 array($k => $v)
或例如 array($k1=>;考虑到地图的实用性, array($k2=>$v))
太长了。 (此外,最近我学习了 JS 的做法,现在我真的嫉妒)
我能想到的最好的解决方法是:
function a() { // array
return func_get_args();
}
and
function h() { // hash
$array=array();
for($i=0; $i<func_num_args()-1; $i+=2) {
$array[func_get_arg($i)]=func_get_arg($i+1);
}
return $array;
}
...但他们不允许使用 =>
运算符。
还有其他想法吗?
I don't know how about you, but I'm not very fond of the way arrays are constructed in PHP. I have this feeling that I use array
keyword way too often and that array($k => $v)
or e.g. array($k1=>array($k2=>$v))
are way too long given usefulness of maps.
(Moreover, recently I've learned JS way of doing it and now I really am jealous)
The best I could come up with to remedy this is:
function a() { // array
return func_get_args();
}
and
function h() { // hash
$array=array();
for($i=0; $i<func_num_args()-1; $i+=2) {
$array[func_get_arg($i)]=func_get_arg($i+1);
}
return $array;
}
...but they don't permit using =>
operator.
Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 PHP 5.4 开始,使用
[
和]
支持数组的简写语法。您的示例:现在可以写为:
Starting in PHP 5.4, a shorthand syntax for arrays is supported using
[
and]
. Your examples:can now be written as:
PHP 中没有声明数组的简写语法。这是我希望看到的功能,但我非常怀疑它会发生。
PHP 开发人员和 PHP 社区对此进行了很多讨论,但从未实现。如果您想了解讨论如何展开,可以在 PHP wiki 上找到一个很好的起点: http:// wiki.php.net/rfc/shortsyntaxforarrays
现在,您将不得不忍受输入一些额外的字符。
There is no shorthand syntax for declaring arrays in PHP. It's a feature I would like to see, but I very much doubt it will happen.
It's been discussed a lot by the PHP developers and the PHP community, but it was never implemented. A good starting point if you want to see how the discussion unfolded is available on the PHP wiki: http://wiki.php.net/rfc/shortsyntaxforarrays
For now, you will have to put up with typing a handful of extra characters.
使用 Texter 或任何带有模板/宏的不错的编辑器。例如:
如果您真的很着迷,请创建一个 json_decode 宏来通过以下方式运行选择:
只是不要将 JSON 放入您的 PHP 代码中,因为您宁愿查看 JSON...
Use Texter or any decent editor with templates/macros. E.g.:
If you're truly obsessed, make a json_decode macro to run a selection through this:
Just don't put JSON in your PHP code because you'd rather look at JSON...