Inconsistent error from PHP about using an object as an array


New member
I am observing an inconsistent error from PHP when using the following statement:

$table_data->attempt_data['attemptarray'][$i]['inputvalue'] = $_POST[$select_tag_name];

(Note: this statement is in a for loop with index $i)

Sometimes this works and sometimes it doesn't. The structure of $table_data is:

$table_data: an object instance
attempt_data: a one-dimensional associative array
attempt_array: a one-dimensional indexed array of objects
inputvalue: a member of the attempt_array element object.

When it doesn't work, PHP throws

"Exception - Cannot use object of type stdClass as array"

I have tried:

$table_data->attempt_data['attemptarray'][$i]->inputvalue = $_POST[$select_tag_name];

But PHP then throws:

"Exception - Attempt to assign property "inputvalue" on array"

In the past I have worked around this by referencing the members of the attempt_array object by casting, i.e,

((array) $table_data->attempt_data['attemptarray'][$I])['inputvalue']

However, in this particular example, the statement is part of a function call in which $table_data is passed by reference:

function update_table_data_input_values_from_POST(&$table_data)

When I tried to use the array cast, PHP threw the following error:

"Fatal error: Cannot use temporary expression in write context"

So, I am stuck between a rock and a hard place. Anyone have a suggestion how I can work around this problem?

Added later: I should have mentioned my environment. I am running PHP 8.1.20 using Apache/2.4.57 on a Macintosh running Ventura (13.4.1)
Last edited:
I have tried to work around this problem without success. I changed the function update_table_data_input_values_from_POST as follows:

function update_table_data_input_values_from_POST(&$attempt_array) {
for ($i=0; $i<count($attempt_array); $i++) {
$interpretation = $attempt_array[$i]->atomicvariablesvalue;
// Strip whitespace from $interpretation

$interpretation = preg_replace('~[\r\n]+~', '', $interpretation);

$problem_id = $attempt_array[$i]->problemid;
$subproblem_id = $attempt_array[$i]->subproblemid;
$select_tag_name = $interpretation . '-' . $problem_id . '-' . $subproblem_id;

// Strip newline from $select_name

$select_name = preg_replace('~[\r\n]+~', '', $select_tag_name);
($attempt_array[$i])->inputvalue = $_POST[$select_tag_name];

The strip whitespace from $interpretation was necessary because when set from $attempt_array[$i]->atomicvariablesvalue, PHP put a space after the atomicvariablesvalue.

I then called the function as follows:


I thought perhaps making the attemptarray instance the parameter to the call might fix the problem. It did not. I still get:

"Exception - Attempt to assign property "inputvalue" on array"

It seems the PHP parser can't analyze the expression ($attempt_array[$i]) to determine it is an object (rather than an array element). I am beginning to think this is a bug, but am hesitant to file a bug report without some feedback from the forum community.

I also tried using a cast, i.e.,

((object) $attempt_array[$i])->inputvalue

But that did not work. It resulted in the error:

"Fatal error: Cannot use temporary expression in write context"

One more thing about the environment I am using. The PHP code is part of a Moodle plugin I am working on. The version of Moodle is 4.2.1.