有没有更好的 PHP 方法从数组(字典)中通过键获取默认值?
在Python中可以这样做:
foo = {}
assert foo.get('bar', 'baz') == 'baz'
在PHP中可以使用三元运算符,如下所示:
$foo = array();
assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz' );
我正在寻找高尔夫版本。我可以用 PHP 做得更短/更好吗?
更新 [2020 年 3 月]:
assert($foo['bar'] ?? 'baz' == 'baz');
空合并运算符 ??
似乎是 值得今天查看。
在下面的评论中找到(+1)
In Python one can do:
foo = {}
assert foo.get('bar', 'baz') == 'baz'
In PHP one can go for a trinary operator as in:
$foo = array();
assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz' );
I am looking for a golf version. Can I do it shorter/better in PHP?
UPDATE [March 2020]:
assert($foo['bar'] ?? 'baz' == 'baz');
It seems that Null coalescing operator ??
is worth checking out today.
found in the comments below (+1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
随着时间的流逝,PHP 也在不断发展。 PHP 7 现在支持 null合并运算符,
??
:Time passes and PHP is evolving. PHP 7 now supports the null coalescing operator,
??
:我刚刚想出了这个小辅助函数:
这不仅适用于字典,而且适用于所有类型的变量:
为每个引用传递先前未定义的变量不会导致
NOTICE
错误。相反,通过引用传递$var
将定义它并将其设置为null
。如果传递的变量是<代码>空。另请注意垃圾邮件/鸡蛋示例中隐式生成的数组:请注意,即使
$var
是通过引用传递的,get($var)
的结果也将是一个副本$var
的,不是引用。我希望这有帮助!I just came up with this little helper function:
Not only does this work for dictionaries, but for all kind of variables:
Passing a previously undefined variable per reference doesn't cause a
NOTICE
error. Instead, passing$var
by reference will define it and set it tonull
. The default value will also be returned if the passed variable isnull
. Also note the implicitly generated array in the spam/eggs example:Note that even though
$var
is passed by reference, the result ofget($var)
will be a copy of$var
, not a reference. I hope this helps!使用错误控制运算符
@
使用 PHP 5.3 快捷方式版本的三元运算符:Use the error control operator
@
with the PHP 5.3 shortcut version of the ternary operator:我发现创建这样的函数很有用:
并像这样使用它:
本例中的默认值为
null
,但您可以将其设置为您需要的任何值。我希望它有用。
I find it useful to create a function like so:
And use it like this:
The default value in this case is
null
, but you may set it to whatever you need.I hope it is useful.
PHP 5.3 有一个三元运算符的快捷版本:
基本上是
否则,不,没有更短的方法。
PHP 5.3 has a shortcut version of the ternary operator:
which is basically
Otherwise, no, there's no shorter method.
一种“稍微”hacky的方法:
http://codepad.viper-7.com/flXHCH
显然这并不是一个好的方法。但在其他情况下它很方便。例如,我经常声明 GET 和 POST 变量的快捷方式,如下所示:
PS:人们可以称其为“内置
==$_=&
特殊比较运算符”:PPS:嗯,你显然可以只是使用
,但这比
==$_=&
运算符更糟糕。你知道,人们不太喜欢错误抑制运算符。A "slightly" hacky way to do it:
http://codepad.viper-7.com/flXHCH
Obviously this isn't really the nice way to do it. But it is handy in other situations. E.g. I often declare shortcuts to GET and POST variables like that:
PS: One could call this the "built-in
==$_=&
special comparison operator":PPS: Well, you could obviously just use
but that's even worse than the
==$_=&
operator. People don't like the error suppression operator much, you know.如果您通过数组中的键枚举默认值,可以通过以下方式完成:
其结果是:
您枚举默认值的键/值对越多,代码高尔夫就变得越好。
If you enumerate the default values by key in an array, it can be done this way:
Which results in:
The more key/value pairs that you enumerate defaults for, the better the code-golf becomes.
“Marc B”提出了一个解决方案,使用三元快捷方式
$x = $foo ?: 'defaultvaluehere ';
但它仍然给出通知。可能是打字错误,也许他的意思是?或者它是在 PHP 7 发布之前编写的。根据三元描述:
但它并不在内部使用
isset
并生成通知。为了避免出现通知,最好使用 空合并运算符
??
在其中使用isset
。在 PHP 7 中可用。There was a solution proposed by "Marc B" to use ternary shortcut
$x = $foo ?: 'defaultvaluehere';
but it still gives notices. Probably it's a mistyping, maybe he meant ?? or it were written before PHP 7 release.According to Ternary description:
But it doesn't use
isset
inside and produces notices.To avoid notices better to use Null Coalescing Operator
??
which usesisset
inside it. Available in PHP 7.