My question is a variation of the following post here added to JSON logic. I´m using PHP 5.6.22
I need to build a class without an object at runtim to build a JSON object of same type to be returned to the client. My code:
public static function getData()
{
$columns = array ("Name", "Address", "Age" );
$values = array ("John", "A Ave 222", 32 );
$ret = (object)[];
$index = 0;
foreach ($columns as $col)
{
$value = $values[$index++];
$ret[] = (object) [$col => $value]; // Error here
}
return json_encode($ret);
}
When running that I get the following error:
<b>Fatal error</b>: Cannot use object of type stdClass as array in <source filename> on line <linenumber>
Help appreaciated to solve that...