Search code examples
phparraysstdclass

Accessing an object variable with # and space in its name in an array


The $this->groups array prints out the following:

Array
(
    [# Chapters] => stdClass Object
        (
            [editable] => 
            [id] => 6
            [title] => Sigma Kappa

I am trying to catch the [title] variable directly, without looping through the array. The issue is with the name of the # Chapters object. Is it somehow possible ?


Solution

  • Since its an array you can access it directly by its key. The first level (which is the parent) is an array, so access it thru ['the_key'], then second level, is an object, so use ->property/attribute Consider this example:

    echo $this->groups['# Chapters']->title;