use Google\Cloud\Spanner\ArrayType;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\StructType;
$structType = new StructType([
[
'name' => 'stringField',
'type' => Database::TYPE_STRING
],
[
'name' => 'arrayField',
'type' => Database::TYPE_ARRAY,
'child' => new ArrayType(Database::TYPE_STRING)
]
]);
Parameter
Name
Description
fields
array[]
An array containing a field definition. Each field
must be of form [(string|null) $name, (int) $type, (ArrayType|StructType|null) $child].
add
Add a struct field definition.
Unnamed struct fields may be defined by providing null as the first
argument value, however you may find it more convenient to use the provided
StructType::addUnnamed() method.
// If a field is a struct or array, it must be explicitly defined.
use Google\Cloud\Spanner\ArrayType;
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\StructType;
// Create a struct which will be nested later.
$customer = (new StructType)
->add('name', Database::TYPE_STRING)
->add('phone', Database::TYPE_STRING)
->add('email', Database::TYPE_STRING)
->add('lastOrderDate', Database::TYPE_DATE);
// Create an array to nest within the customer type definition.
$orderIds = new ArrayType(Database::TYPE_INT64);
$customer->add('orderIds', $orderIds);
// Add the customer definition to the parameter definition.
$structType->add('customer', $customer);
$type A value type code or nested
struct or array definition. Accepted integer values are defined as
constants on Database, and are as
follows: Database::TYPE_BOOL, Database::TYPE_INT64,
Database::TYPE_FLOAT64, Database::TYPE_TIMESTAMP,
Database::TYPE_DATE, Database::TYPE_STRING and
Database::TYPE_BYTES
$type A value type code or nested
struct or array definition. Accepted integer values are defined as
constants on Database, and are as
follows: Database::TYPE_BOOL, Database::TYPE_INT64,
Database::TYPE_FLOAT64, Database::TYPE_TIMESTAMP,
Database::TYPE_DATE, Database::TYPE_STRING and
Database::TYPE_BYTES
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Spanner Client - Class StructType (1.104.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.104.0 (latest)](/php/docs/reference/cloud-spanner/latest/StructType)\n- [1.103.0](/php/docs/reference/cloud-spanner/1.103.0/StructType)\n- [1.102.0](/php/docs/reference/cloud-spanner/1.102.0/StructType)\n- [1.101.0](/php/docs/reference/cloud-spanner/1.101.0/StructType)\n- [1.100.0](/php/docs/reference/cloud-spanner/1.100.0/StructType)\n- [1.98.0](/php/docs/reference/cloud-spanner/1.98.0/StructType)\n- [1.97.0](/php/docs/reference/cloud-spanner/1.97.0/StructType)\n- [1.96.0](/php/docs/reference/cloud-spanner/1.96.0/StructType)\n- [1.95.0](/php/docs/reference/cloud-spanner/1.95.0/StructType)\n- [1.94.0](/php/docs/reference/cloud-spanner/1.94.0/StructType)\n- [1.93.1](/php/docs/reference/cloud-spanner/1.93.1/StructType)\n- [1.92.1](/php/docs/reference/cloud-spanner/1.92.1/StructType)\n- [1.91.0](/php/docs/reference/cloud-spanner/1.91.0/StructType)\n- [1.90.0](/php/docs/reference/cloud-spanner/1.90.0/StructType)\n- [1.89.0](/php/docs/reference/cloud-spanner/1.89.0/StructType)\n- [1.88.0](/php/docs/reference/cloud-spanner/1.88.0/StructType)\n- [1.87.0](/php/docs/reference/cloud-spanner/1.87.0/StructType)\n- [1.86.0](/php/docs/reference/cloud-spanner/1.86.0/StructType)\n- [1.85.0](/php/docs/reference/cloud-spanner/1.85.0/StructType)\n- [1.84.0](/php/docs/reference/cloud-spanner/1.84.0/StructType)\n- [1.83.0](/php/docs/reference/cloud-spanner/1.83.0/StructType)\n- [1.82.0](/php/docs/reference/cloud-spanner/1.82.0/StructType)\n- [1.81.0](/php/docs/reference/cloud-spanner/1.81.0/StructType)\n- [1.80.0](/php/docs/reference/cloud-spanner/1.80.0/StructType)\n- [1.79.0](/php/docs/reference/cloud-spanner/1.79.0/StructType)\n- [1.78.0](/php/docs/reference/cloud-spanner/1.78.0/StructType)\n- [1.77.0](/php/docs/reference/cloud-spanner/1.77.0/StructType)\n- [1.76.1](/php/docs/reference/cloud-spanner/1.76.1/StructType)\n- [1.68.0](/php/docs/reference/cloud-spanner/1.68.0/StructType)\n- [1.67.0](/php/docs/reference/cloud-spanner/1.67.0/StructType)\n- [1.66.0](/php/docs/reference/cloud-spanner/1.66.0/StructType)\n- [1.65.0](/php/docs/reference/cloud-spanner/1.65.0/StructType)\n- [1.64.0](/php/docs/reference/cloud-spanner/1.64.0/StructType)\n- [1.63.2](/php/docs/reference/cloud-spanner/1.63.2/StructType)\n- [1.62.1](/php/docs/reference/cloud-spanner/1.62.1/StructType)\n- [1.61.0](/php/docs/reference/cloud-spanner/1.61.0/StructType)\n- [1.60.0](/php/docs/reference/cloud-spanner/1.60.0/StructType)\n- [1.59.0](/php/docs/reference/cloud-spanner/1.59.0/StructType)\n- [1.58.4](/php/docs/reference/cloud-spanner/1.58.4/StructType)\n- [1.57.0](/php/docs/reference/cloud-spanner/1.57.0/StructType)\n- [1.56.0](/php/docs/reference/cloud-spanner/1.56.0/StructType)\n- [1.55.0](/php/docs/reference/cloud-spanner/1.55.0/StructType)\n- [1.54.2](/php/docs/reference/cloud-spanner/1.54.2/StructType) \nReference documentation and code samples for the Cloud Spanner Client class StructType.\n\nRepresents a Struct Query Parameter type declaration.\n\nExample: \n\n use Google\\Cloud\\Spanner\\StructType;\n use Google\\Cloud\\Spanner\\Database;\n use Google\\Cloud\\Spanner\\SpannerClient;\n\n $spanner = new SpannerClient();\n $database = $spanner-\u003econnect('my-instance', 'my-database');\n\n $res = $database-\u003eexecute('SELECT @userStruct.firstName, @userStruct.lastName', [\n 'parameters' =\u003e [\n 'userStruct' =\u003e [\n 'firstName' =\u003e 'John',\n 'lastName' =\u003e 'Testuser'\n ]\n ],\n 'types' =\u003e [\n 'userStruct' =\u003e (new StructType())\n -\u003eadd('firstName', Database::TYPE_STRING)\n -\u003eadd('lastName', Database::TYPE_STRING)\n ]\n ])-\u003erows()-\u003ecurrent();\n\n $fullName = $res['firstName'] . ' ' . $res['lastName']; // `John Testuser`\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Spanner\n\nMethods\n-------\n\n### __construct\n\nExample: \n\n use Google\\Cloud\\Spanner\\ArrayType;\n use Google\\Cloud\\Spanner\\Database;\n use Google\\Cloud\\Spanner\\StructType;\n\n $structType = new StructType([\n [\n 'name' =\u003e 'stringField',\n 'type' =\u003e Database::TYPE_STRING\n ],\n [\n 'name' =\u003e 'arrayField',\n 'type' =\u003e Database::TYPE_ARRAY,\n 'child' =\u003e new ArrayType(Database::TYPE_STRING)\n ]\n ]);\n\n### add\n\nAdd a struct field definition.\n\nUnnamed struct fields may be defined by providing `null` as the first\nargument value, however you may find it more convenient to use the provided\n[StructType::addUnnamed()](/php/docs/reference/cloud-spanner/latest/StructType#_Google_Cloud_Spanner_StructType__addUnnamed__) method.\n\nExample: \n\n $structType-\u003eadd('firstName', Database::TYPE_STRING);\n\n // If a field is a struct or array, it must be explicitly defined.\n use Google\\Cloud\\Spanner\\ArrayType;\n use Google\\Cloud\\Spanner\\Database;\n use Google\\Cloud\\Spanner\\StructType;\n\n // Create a struct which will be nested later.\n $customer = (new StructType)\n -\u003eadd('name', Database::TYPE_STRING)\n -\u003eadd('phone', Database::TYPE_STRING)\n -\u003eadd('email', Database::TYPE_STRING)\n -\u003eadd('lastOrderDate', Database::TYPE_DATE);\n\n // Create an array to nest within the customer type definition.\n $orderIds = new ArrayType(Database::TYPE_INT64);\n $customer-\u003eadd('orderIds', $orderIds);\n\n // Add the customer definition to the parameter definition.\n $structType-\u003eadd('customer', $customer);\n\n### addUnnamed\n\nAdd a struct field with no name.\n\nExample: \n\n $structType-\u003eaddUnnamed(Database::TYPE_STRING);\n\n### fields\n\nFetch the defined fields list."]]