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