2

So I have an object $entity, and a property $property.

$property has several arrays inside of it.

I want to be able to unset these arrays, as such:

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

However, I'm getting the error:

Error: Cannot use string offset as an array

I tried setting $entity->$property to a single variable, but $entity was passed by reference into the function, and changing a new variable isn't going to modify the old object.

How do I ensure the old object gets modified and these arrays get unset?

Here is part of the entity, up until the property:

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 is field_file, and looks like this:

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 ) ) )
5
  • 1
    Your class code to see the structure would help here. Commented Jun 16, 2014 at 19:32
  • No, I meant, I set an $entity_property variable for $entity->$property. Modifying that isn't going to reflect back into $entity. Commented Jun 16, 2014 at 19:42
  • 1
    Hope this helps: 3v4l.org/iWPea Commented Jun 16, 2014 at 19:43
  • Ah, now I see. In $entity->$property['array'] expression, array access is processed first. So it technically reads as $entity->($property['array']). Commented Jun 16, 2014 at 19:45
  • No, that will not help hek2mgl. That's precisely what I am trying to do, except that the property is inside a variable. Commented Jun 16, 2014 at 19:49

1 Answer 1

3

It should be written like this instead:

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

Without the curly brackets, the array access operator is bound more tightly. Hence your original expression will actually be processed as...

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

As $property itself is a string, it's obviously wrong to treat as an array - that's what PHP warns you about.

Sign up to request clarification or add additional context in comments.

2 Comments

This fixed the issue. Thank you. Do you have a link to the relevant page describing this on PHP's website?
No, and that's weird; operator precedence table somehow omits -> completely. What I've found is this article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.