Reference documentation and code samples for the Cloud Spanner Client class StructValue.
Defines a struct parameter value with its fields.
This class is included to provide a fluent interface to build complex struct
parameter values for your queries against Cloud Spanner. If your struct does
or may include unnamed fields, or duplicate field names, you must use a
StructValue. In most cases, however, an associative array may be used.
Please note that query results expressed as structs will not be returned using
this class. Query results will always be expressed as a PHP array. This class
is intended to be used to create complex struct values only.
If your query contains duplicate field names, it must be returned as a name/value
pair as demonstrated in the example below usingResult::RETURN_NAME_VALUE_PAIR.
Example:
use Google\Cloud\Spanner\Database;
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Result;
use Google\Cloud\Spanner\StructType;
use Google\Cloud\Spanner\StructValue;
$spanner = new SpannerClient();
$database = $spanner->connect('my-instance', 'my-database');
$res = $database->execute('SELECT * FROM UNNEST(ARRAY(SELECT @structParam))', [
'parameters' => [
'structParam' => (new StructValue)
->add('foo', 'bar')
->add('foo', 2)
->addUnnamed('this field is unnamed')
],
'types' => [
'structParam' => (new StructType)
->add('foo', Database::TYPE_STRING)
->add('foo', Database::TYPE_INT64)
->addUnnamed(Database::TYPE_STRING)
]
])->rows(Result::RETURN_NAME_VALUE_PAIR)->current();
echo $res[0]['name'] . ': ' . $res[0]['value'] . PHP_EOL; // "foo: bar"
echo $res[1]['name'] . ': ' . $res[1]['value'] . PHP_EOL; // "foo: 2"
echo $res[2]['name'] . ': ' . $res[2]['value'] . PHP_EOL; // "2: this field is unnamed"
Namespace
Google \ Cloud \ Spanner
Methods
__construct
Parameter
Name
Description
values
array[]
An array containing a field value. Each value
must be of form[(string|null) $name, mixed $value].
[[["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 StructValue (1.104.0)\n\nVersion latestkeyboard_arrow_down\n\n- [1.104.0 (latest)](/php/docs/reference/cloud-spanner/latest/StructValue)\n- [1.103.0](/php/docs/reference/cloud-spanner/1.103.0/StructValue)\n- [1.102.0](/php/docs/reference/cloud-spanner/1.102.0/StructValue)\n- [1.101.0](/php/docs/reference/cloud-spanner/1.101.0/StructValue)\n- [1.100.0](/php/docs/reference/cloud-spanner/1.100.0/StructValue)\n- [1.98.0](/php/docs/reference/cloud-spanner/1.98.0/StructValue)\n- [1.97.0](/php/docs/reference/cloud-spanner/1.97.0/StructValue)\n- [1.96.0](/php/docs/reference/cloud-spanner/1.96.0/StructValue)\n- [1.95.0](/php/docs/reference/cloud-spanner/1.95.0/StructValue)\n- [1.94.0](/php/docs/reference/cloud-spanner/1.94.0/StructValue)\n- [1.93.1](/php/docs/reference/cloud-spanner/1.93.1/StructValue)\n- [1.92.1](/php/docs/reference/cloud-spanner/1.92.1/StructValue)\n- [1.91.0](/php/docs/reference/cloud-spanner/1.91.0/StructValue)\n- [1.90.0](/php/docs/reference/cloud-spanner/1.90.0/StructValue)\n- [1.89.0](/php/docs/reference/cloud-spanner/1.89.0/StructValue)\n- [1.88.0](/php/docs/reference/cloud-spanner/1.88.0/StructValue)\n- [1.87.0](/php/docs/reference/cloud-spanner/1.87.0/StructValue)\n- [1.86.0](/php/docs/reference/cloud-spanner/1.86.0/StructValue)\n- [1.85.0](/php/docs/reference/cloud-spanner/1.85.0/StructValue)\n- [1.84.0](/php/docs/reference/cloud-spanner/1.84.0/StructValue)\n- [1.83.0](/php/docs/reference/cloud-spanner/1.83.0/StructValue)\n- [1.82.0](/php/docs/reference/cloud-spanner/1.82.0/StructValue)\n- [1.81.0](/php/docs/reference/cloud-spanner/1.81.0/StructValue)\n- [1.80.0](/php/docs/reference/cloud-spanner/1.80.0/StructValue)\n- [1.79.0](/php/docs/reference/cloud-spanner/1.79.0/StructValue)\n- [1.78.0](/php/docs/reference/cloud-spanner/1.78.0/StructValue)\n- [1.77.0](/php/docs/reference/cloud-spanner/1.77.0/StructValue)\n- [1.76.1](/php/docs/reference/cloud-spanner/1.76.1/StructValue)\n- [1.68.0](/php/docs/reference/cloud-spanner/1.68.0/StructValue)\n- [1.67.0](/php/docs/reference/cloud-spanner/1.67.0/StructValue)\n- [1.66.0](/php/docs/reference/cloud-spanner/1.66.0/StructValue)\n- [1.65.0](/php/docs/reference/cloud-spanner/1.65.0/StructValue)\n- [1.64.0](/php/docs/reference/cloud-spanner/1.64.0/StructValue)\n- [1.63.2](/php/docs/reference/cloud-spanner/1.63.2/StructValue)\n- [1.62.1](/php/docs/reference/cloud-spanner/1.62.1/StructValue)\n- [1.61.0](/php/docs/reference/cloud-spanner/1.61.0/StructValue)\n- [1.60.0](/php/docs/reference/cloud-spanner/1.60.0/StructValue)\n- [1.59.0](/php/docs/reference/cloud-spanner/1.59.0/StructValue)\n- [1.58.4](/php/docs/reference/cloud-spanner/1.58.4/StructValue)\n- [1.57.0](/php/docs/reference/cloud-spanner/1.57.0/StructValue)\n- [1.56.0](/php/docs/reference/cloud-spanner/1.56.0/StructValue)\n- [1.55.0](/php/docs/reference/cloud-spanner/1.55.0/StructValue)\n- [1.54.2](/php/docs/reference/cloud-spanner/1.54.2/StructValue) \nReference documentation and code samples for the Cloud Spanner Client class StructValue.\n\nDefines a struct parameter value with its fields.\n\nThis class is included to provide a fluent interface to build complex struct\nparameter values for your queries against Cloud Spanner. If your struct does\nor may include unnamed fields, or duplicate field names, you must use a\nStructValue. In most cases, however, an associative array may be used.\n\nPlease note that query results expressed as structs will not be returned using\nthis class. Query results will always be expressed as a PHP array. This class\nis intended to be used to create complex struct values only.\n\nIf your query contains duplicate field names, it must be returned as a name/value\npair as demonstrated in the example below using `Result::RETURN_NAME_VALUE_PAIR`.\n\nExample: \n\n use Google\\Cloud\\Spanner\\Database;\n use Google\\Cloud\\Spanner\\SpannerClient;\n use Google\\Cloud\\Spanner\\Result;\n use Google\\Cloud\\Spanner\\StructType;\n use Google\\Cloud\\Spanner\\StructValue;\n\n $spanner = new SpannerClient();\n $database = $spanner-\u003econnect('my-instance', 'my-database');\n\n $res = $database-\u003eexecute('SELECT * FROM UNNEST(ARRAY(SELECT @structParam))', [\n 'parameters' =\u003e [\n 'structParam' =\u003e (new StructValue)\n -\u003eadd('foo', 'bar')\n -\u003eadd('foo', 2)\n -\u003eaddUnnamed('this field is unnamed')\n ],\n 'types' =\u003e [\n 'structParam' =\u003e (new StructType)\n -\u003eadd('foo', Database::TYPE_STRING)\n -\u003eadd('foo', Database::TYPE_INT64)\n -\u003eaddUnnamed(Database::TYPE_STRING)\n ]\n ])-\u003erows(Result::RETURN_NAME_VALUE_PAIR)-\u003ecurrent();\n\n echo $res[0]['name'] . ': ' . $res[0]['value'] . PHP_EOL; // \"foo: bar\"\n echo $res[1]['name'] . ': ' . $res[1]['value'] . PHP_EOL; // \"foo: 2\"\n echo $res[2]['name'] . ': ' . $res[2]['value'] . PHP_EOL; // \"2: this field is unnamed\"\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Spanner\n\nMethods\n-------\n\n### __construct\n\n### add\n\nAdd a single value to the struct.\n\nExample: \n\n $structValue-\u003eadd('firstName', 'John');\n\n### addUnnamed\n\nAdd an unnamed value to the struct.\n\nExample: \n\n $structValue-\u003eaddUnnamed('John');\n\n### values\n\nGet the list of values."]]