0

I've got an odd error in my PHP code regarding dynamic arrays.

The error outputted is:

Fatal error: Cannot use string offset as an array ... on line 89

This is a portion of my code, it is within a foreach loop, which is looping through settings in a database:

foreach($query->fetchAll() as $row)
{
    if($site!=CURRENT_SITE_TEMPLATE)
    {
        $property = 'foreignSettings';
        $propertyType = 'foreignSettingsTypes';
    } else {
        $property = 'settings';
        $propertyType = 'settingTypes';
    }

    $this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];
    settype($this->$property[$row['variable_section']][$row['variable_name']],$row['variable_type']);

    $this->$propertyType[$row['variable_section']][$row['variable_name']] = $row['variable_type'];
}

For the sake of the example code, $site is 'admin' and CURRENT_SITE_TEMPLATE is 'admin'. In addition, $foreignSettings, $foreignSettingsTypes, $settings, and $settingTypes are all defined as arrays in the class scope

The error is on line 89, which is:

$this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];

I originally thought it was because of the $property variable accesing the array, however, this looks like valid legal code in the PHP documentation ( http://php.net/manual/en/language.variables.variable.php in example #1)

Any help on this error would be appreciated.

Thanks

3
  • 1
    Is the settings or foreignSettings property an array? var_dump($this->$property) and check what is in there. Commented Mar 21, 2013 at 11:50
  • I don't know how it should be, but try one of the following and see if it is ok: $this->{$property[$row['variable_section']][$row['variable_name']]} or $this->{$property}[$row['variable_section']][$row['variable_name']] (the 2nd one should be ok) Commented Mar 21, 2013 at 12:00
  • yeah right the second one should do it Commented Mar 21, 2013 at 12:03

2 Answers 2

1

In your given example $property is a string. You are then trying to use that as an array. Strings only has numeric indexes (if you need to use as an array).

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

Comments

0

The problem is as follows: $this->$property[0] means you access the 0th place of $property which in your case would be the first letter of the string $property. Thus you end up with $this->f or $this->s.

with $this->$property[0][0] you would be trying to access the 0th place of the 0th place of the $property string what results in an error because you try to access the 0th place of the char s what is not possible since the char s can not be referenced as an array.

what you want is $this->{$propperty}[0][0] what means that you try to access the 0th place of the 0th place of the variable that has the name $propperty.

Comments

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.