Search code examples
phpstdclass

Notice: Array to string conversion when inserting a stdClass into a stdClass


I am trying to insert data from a database into a stdClass but for some reason, I get a notice Notice: Array to string conversion in data.php on line 47.

I'm creating 2 stdClasses, one for each vehicle and one general that contains all of them. When I'm trying to insert the vehicle's stdClass into the general one I get the notice.

Here is my code (Notice I wrote where line 47 is):

$data = new stdClass();

while($row = mysqli_fetch_array($tresult)) {
    $vehicle = new stdClass();
    $vehicle->name = $row['name'];
    $vehicle->position = $row['position'];

    $data->$row['name'] = $vehicle; //Line 47
}

What am I missing here? Thank you


Solution

  • Try to assign this way, because you have to interpolate the variable

    $data->{$row['name']} = $vehicle