简体   繁体   English

在PHP中使用`/`作为数组索引,然后强制转换为对象

[英]Using `/` as an array index in PHP, then casting to object

I have an array that is defined as such: 我有这样定义的数组:

$bar = array(
   '/' => 'foo'
);

I can convert it to a class (using $baz = (object) $bar ), and I don't get any errors. 我可以将其转换为类(使用$baz = (object) $bar ),并且不会出现任何错误。 $baz contains the following: (via var_dump ) $baz包含以下内容:(通过var_dump

object(stdClass)#1 (1) {
  ["/"]=>
  string(3) "foo"
}

However, I can't seem to find a way to access the / property of $baz . 但是,我似乎找不到找到访问$baz/属性的方法。 I thought I could use $baz -> /; 我以为我可以用$baz -> /; , but obviously there are syntax issues with that statement. ,但该语句显然存在语法问题。

Thanks for the help! 谢谢您的帮助!

Try this syntax 试试这个语法

$baz->{'/'};

http://php.net/manual/en/language.variables.variable.php http://php.net/manual/zh/language.variables.variable.php

Curly braces may also be used, to clearly delimit the property name. 也可以使用大括号来清楚地界定属性名称。 They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid 当访问包含数组的属性中的值,属性名称由多个部分组成或属性名称包含其他无效字符时,它们最有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM