所以我有一个对象$ entity和一个属性$ property。
$ property里面有几个数组。
我希望能够取消设置这些数组:
unset($entity->$property['array']);
但是,我收到了错误:
Error: Cannot use string offset as an array
我尝试将$ entity-> $属性设置为单个变量,但$ entity通过引用传递给函数,而更改新变量则不会修改旧对象。
如何确保修改旧对象并取消设置这些数组?
这是实体的一部分,直到属性:
stdClass Object
(
[vid] => 33128
[uid] => 3
[title] => Title
[log] =>
[status] => 0
[comment] => 0
[promote] => 1
[sticky] => 0
[vuuid] => 3f922255-462b-459a-a8d6-9a2ac30899cf
[hash] => fa14fb00f1355761429977fe916e8f66
[nid] => 29652
[type] => resource
[language] => en-US
[created] => 1137158100
[changed] => 1371649725
[tnid] => 29652
[translate] => 0
[uuid] => 051eba0c-4b52-4269-b0d2-4d0ced9d6a6e
[revision_timestamp] => 1371649725
[revision_uid] => 0
[body] => Array
(
)
[field_file] => Array
(
[und] => Array
(
[0] => Array
(
[fid] => 26750
[uid] => 1
[filename] => filename.pdf
[uri] => public://cms/cms/filepath/filename.pdf
[filemime] => application/pdf
[filesize] => 666960
[status] => 1
[timestamp] => 1370660770
[type] => default
[uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793
[hash] => aafc3f362cda639dc0475fccbeeb9af2
[rdf_mapping] => Array
(
)
[display] => 1
[description] => filename.pdf
)
)
)
$ property是field_file,如下所示:
Array ( [und] => Array ( [0] => Array ( [fid] => 11285 [uid] => 1 [filename] => filename.pdf [uri] => public://cms/cms/resource_library/datasheets/filepath/filename.pdf [filemime] => application/pdf [filesize] => 666960 [status] => 1 [timestamp] => 1368086259 [type] => default [uuid] => ea7c2c1f-4ef2-44fb-b89d-51c0fa090793 [hash] => cdb2fc5203a9e7f9c066a89bf9f70502 [rdf_mapping] => Array ( ) [display] => 1 [description] => filename.pdf ) ) )
答案 0 :(得分:3)
应该这样写:
unset($entity->{$property}['some_array_name']);
如果没有大括号,数组访问运算符的绑定会更紧密。因此,您的原始表达式实际上将被处理为...
unset($entity->($property['some_array_name']));
由于$property
本身就是一个字符串,因此将数据视为数组显然是错误的 - 这就是PHP警告你的。