When working with multiple data groups from a single function call in PHP, what strategies can be used to efficiently convert them into a JSON array?

When working with multiple data groups from a single function call in PHP, one efficient strategy to convert them into a JSON array is to store each data group in an associative array with a unique key, and then encode the entire array into JSON using the json_encode function. This allows you to maintain the structure of the data groups and easily access them when needed.

// Sample data groups
$dataGroup1 = ['name' => 'John', 'age' => 30];
$dataGroup2 = ['name' => 'Jane', 'age' => 25];

// Store data groups in an associative array
$data = [
    'group1' => $dataGroup1,
    'group2' => $dataGroup2
];

// Convert associative array to JSON
$jsonData = json_encode($data);

// Output JSON data
echo $jsonData;