domain_id, $grouped)) { // a new ID... $newObject = new stdClass(); // Copy the ID/ID_NAME, and create an ITEMS placeholder $newObject->_domain_id = $object->domain_id; $newObject->_service_url = $object->service_url; $newObject->_layer_name = $object->layer_name; $newObject->_field_name = $object->field_name; $newObject->_field_domain = array(); // Save this new object $grouped[$object->domain_id] = $newObject; } $taskObject = new stdClass(); // Copy the TASK/TASK_NAME $taskObject->name = $object->domain_name; $taskObject->value = $object->domain_value; // Append this new task to the ITEMS array $grouped[$object->domain_id]->_field_domain[] = $taskObject; } // We use array_values() to remove the keys used to identify similar objects // And then re-encode this data :) $grouped = array_values($grouped); $json = json_encode($grouped); echo $json; pg_free_result($result); pg_close($dbconn); } if ($_GET['db'] == 'sqlite') { $sqlite_db = new PDO('sqlite:' . $sqlite_dbname); $objects = $sqlite_db->query("SELECT * FROM " . $sqlite_table . ' ORDER BY domain_name ASC '); $grouped = array(); // Loop JSON objects foreach ($objects as $object) { if (!array_key_exists($object['domain_id'], $grouped)) { // a new ID... $newObject = new stdClass(); // Copy the ID/ID_NAME, and create an ITEMS placeholder $newObject->_domain_id = $object['domain_id']; $newObject->_service_url = $object['service_url']; $newObject->_layer_name = $object['layer_name']; $newObject->_field_name = $object['field_name']; $newObject->_field_domain = array(); // Save this new object $grouped[$object['domain_id']] = $newObject; } $taskObject = new stdClass(); // Copy the TASK/TASK_NAME $taskObject->name = $object['domain_name']; $taskObject->value = $object['domain_value']; // Append this new task to the ITEMS array $grouped[$object['domain_id']]->_field_domain[] = $taskObject; } // We use array_values() to remove the keys used to identify similar objects // And then re-encode this data :) $grouped = array_values($grouped); $json = json_encode($grouped); echo $json; // Close file db connection $sqlite_db = null; } }