Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 取消设置变量属性上的数组_Php_Arrays_Object - Fatal编程技术网

Php 取消设置变量属性上的数组

Php 取消设置变量属性上的数组,php,arrays,object,Php,Arrays,Object,所以我有一个对象$entity和一个属性$property $property中有几个数组 我希望能够取消设置这些阵列,例如: unset($entity->$property['array']); 但是,我得到了一个错误: Error: Cannot use string offset as an array 我尝试将$entity->$属性设置为单个变量,但$entity是通过引用传递到函数中的,更改新变量不会修改旧对象 如何确保旧对象得到修改,这些数组得到取消设置 以下是实体的一

所以我有一个对象$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_文件,如下所示:

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 ) ) )

应该这样写:

unset($entity->{$property}['some_array_name']);
如果没有花括号,数组访问操作符将绑定得更紧密。因此,您的原始表达式实际上将被处理为

unset($entity->($property['some_array_name']));

由于
$property
本身是一个字符串,将其视为数组显然是错误的—这是PHP警告您的。

您的类代码可以查看该结构。不,我的意思是,我为$entity->$property设置了$entity\u属性变量。修改它不会反映回$entity。希望这有帮助:啊,现在我明白了。在
$entity->$property['array']
表达式中,首先处理数组访问。所以从技术上讲,它读作
$entity->($property['array'])
。不,这对hek2mgl没有帮助。这正是我想要做的,只是属性在变量中。这解决了问题。非常感谢。你有PHP网站上描述这一点的相关页面的链接吗?没有,这很奇怪;不知何故完全忽略了
->
。我发现的是。