Convert an array into an array of objects in php

This might be an easy one but I'm struggling to get it done.

I have this following array in php which I want to json_encode as an array of objects. What's the best way of doing this in php?

Array
(
    [6946] => Array
        (
            [AssetGroupName] => Computer
            [AssetGroupID] => 14
        )

    [6945] => Array
        (
            [AssetGroupName] => Laptop
            [AssetGroupID] => 15
        )
)

Json output I'm trying to get.

{ 
   "data": [
      {
         "AssetGroupName": "Computer",
         "AssetGroupID": "12"
      },
      {
         "AssetGroupName": "Laptop",
         "AssetGroupID": "15"
      }
   ]
}


Read more here: https://stackoverflow.com/questions/66995896/convert-an-array-into-an-array-of-objects-in-php

Content Attribution

This content was originally published by Florian at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: