PHP 向 google 工作表添加行时的数组异常
PHP array oddity when adding rows to google sheets
我已经用了几个小时 google 和反复试验,但找不到答案或好的解决方案。我的编码技能不是很好,所以这对专家来说可能是显而易见的......请有人告诉我发生了什么事:D
我正在将网上商店的订单写到 google 张。为了让东西按正确的顺序排列,我创建了数组 $temparray,然后将其馈送到 google sheets.
出于某种原因,上传仅在我对内部数组执行这种令人讨厌的内爆-爆炸技巧时才有效。 (经过几个小时的挫折,我开始尝试一些随机的东西,发现这很有帮助)
Wtf 是不是我的数组有问题?
foreach ($_SESSION["ticketdata"] as $key => $value) {
if ($_SESSION["debug"] == 1) {
print $key." ";
}
$temparray[$key-1][0] = $_SESSION["ticketdata"][$key]["ticketid"];
$temparray[$key-1][1] = $_SESSION["tickettype"];
$temparray[$key-1][2] = $_SESSION["ticketdata"][$key]["ticketname-l"];
$temparray[$key-1][3] = $_SESSION["ticketdata"][$key]["ticketname-f"];
$temparray[$key-1][4] = $_SESSION["ticketdata"][$key]["ticketemail"];
$temparray[$key-1][5] = $_SESSION["ticketdata"][$key]["ticketemployer"];
$temparray[$key-1][6] = $_SESSION["ticketdata"][$key]["prews"];
$temparray[$key-1][7] = $_SESSION["ticketdata"][$key]["ws26"];
$temparray[$key-1][8] = $_SESSION["ticketdata"][$key]["ws27"];
$temparray[$key-1][9] = $_SESSION["ticketdata"][$key]["diet-lactose"];
$temparray[$key-1][10] = $_SESSION["ticketdata"][$key]["diet-gluten"];
$temparray[$key-1][11] = $_SESSION["ticketdata"][$key]["diet-open"];
$temparray[$key-1][12] = $_SESSION["orderid"];
if ($key == 1) {
$temparray[$key-1][13] = $_SESSION["totalprice"];
$temparray[$key-1][14] = ""; // placeholder
$temparray[$key-1][15] = $_SESSION["bill-email"];
$temparray[$key-1][16] = $_SESSION["bill-type"];
$temparray[$key-1][17] = $_SESSION["bill-v-address"];
$temparray[$key-1][18] = $_SESSION["bill-v-operator"];
$temparray[$key-1][19] = $_SESSION["bill-additional"];
$temparray[$key-1][20] = ""; // placeholder
$temparray[$key-1][21] = ""; // placeholder
$temparray[$key-1][22] = $_SESSION["customername-l"];
$temparray[$key-1][23] = $_SESSION["customername-f"];
$temparray[$key-1][24] = $_SESSION["customeremail"];
$temparray[$key-1][25] = $_SESSION["customerphone"];
$temparray[$key-1][26] = $_SESSION["customeremployer"];
$temparray[$key-1][27] = date('Y-m-d H:i:s');
}
$temparraystr = implode("°§",$temparray[$key-1]); // Why do I need to do this?
$temparray[$key-1] = explode("°§",$temparraystr); // mitä *ttua miksei toimi muuten????
}
// the google sheets part:
// getClient() copypasted from here: https://developers.google.com/sheets/api/quickstart/php
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'sheet id not shown on stack overflow :)';
$range = "tilaukset!A3:AB";
$valueRange = new Google_Service_Sheets_ValueRange();
$valueRange->setValues($temparray);
$conf = ["valueInputOption" => "RAW"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
如果没有诡计,我会收到这条不太有用的错误消息
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with
message '{\n "error": {\n "code": 400,\n "message": "Invalid JSON
payload received. Unknown name \"1\" at 'data.values': Cannot find
field.\nInvalid JSON payload received. Unknown name \"2\" at
'data.values': Cannot find field.",\n "errors": [\n {\n
"message": "Invalid JSON payload received. Unknown name \"1\"
at 'data.values': Cannot find field.\nInvalid JSON payload received.
Unknown name \"2\" at 'data.values': Cannot find field.",\n
"domain": "global",\n "reason": "badRequest"\n }\n
],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
print_r($temparray) 输出似乎与或不使用内爆-爆炸技巧完全相同。这个:
Array
(
[0] => Array
(
[0] => SfGotLbD-1545531504-HZ2WGmpJ
[1] => evy
[2] => Secondname
[3] => Firstname
[4] => email@host
[5] => employer
[6] => 1
[7] => ph
[8] => ekg
[9] =>
[10] =>
[11] =>
[12] => 1545531504-HZ2WGmpJ
[13] => 1050
[14] =>
[15] => email2@host
[16] => verkkolasku
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] => Secondname
[23] => Firstname
[24] => email@host
[25] => 555 555555
[26] => employer
[27] => 2018-12-23 04:20:04
)
)
问题是数组中的数据类型错误。它需要是字符串。
数据通过 strval()
以确保它被转换为字符串,这有效。
对于已知为字符串的数据,这不是强制性的。
示例:
$temparray[$key-1][0] = strval($_SESSION["ticketdata"][$key]["ticketid"]);
$temparray[$key-1][1] = strval($_SESSION["tickettype"]);
$temparray[$key-1][2] = strval($_SESSION["ticketdata"][$key]["ticketname-l"]);
$temparray[$key-1][3] = strval($_SESSION["ticketdata"][$key]["ticketname-f"]);
$temparray[$key-1][4] = strval($_SESSION["ticketdata"][$key]["ticketemail"]);
$temparray[$key-1][5] = strval($_SESSION["ticketdata"][$key]["ticketemployer"]);
我已经用了几个小时 google 和反复试验,但找不到答案或好的解决方案。我的编码技能不是很好,所以这对专家来说可能是显而易见的......请有人告诉我发生了什么事:D
我正在将网上商店的订单写到 google 张。为了让东西按正确的顺序排列,我创建了数组 $temparray,然后将其馈送到 google sheets.
出于某种原因,上传仅在我对内部数组执行这种令人讨厌的内爆-爆炸技巧时才有效。 (经过几个小时的挫折,我开始尝试一些随机的东西,发现这很有帮助)
Wtf 是不是我的数组有问题?
foreach ($_SESSION["ticketdata"] as $key => $value) {
if ($_SESSION["debug"] == 1) {
print $key." ";
}
$temparray[$key-1][0] = $_SESSION["ticketdata"][$key]["ticketid"];
$temparray[$key-1][1] = $_SESSION["tickettype"];
$temparray[$key-1][2] = $_SESSION["ticketdata"][$key]["ticketname-l"];
$temparray[$key-1][3] = $_SESSION["ticketdata"][$key]["ticketname-f"];
$temparray[$key-1][4] = $_SESSION["ticketdata"][$key]["ticketemail"];
$temparray[$key-1][5] = $_SESSION["ticketdata"][$key]["ticketemployer"];
$temparray[$key-1][6] = $_SESSION["ticketdata"][$key]["prews"];
$temparray[$key-1][7] = $_SESSION["ticketdata"][$key]["ws26"];
$temparray[$key-1][8] = $_SESSION["ticketdata"][$key]["ws27"];
$temparray[$key-1][9] = $_SESSION["ticketdata"][$key]["diet-lactose"];
$temparray[$key-1][10] = $_SESSION["ticketdata"][$key]["diet-gluten"];
$temparray[$key-1][11] = $_SESSION["ticketdata"][$key]["diet-open"];
$temparray[$key-1][12] = $_SESSION["orderid"];
if ($key == 1) {
$temparray[$key-1][13] = $_SESSION["totalprice"];
$temparray[$key-1][14] = ""; // placeholder
$temparray[$key-1][15] = $_SESSION["bill-email"];
$temparray[$key-1][16] = $_SESSION["bill-type"];
$temparray[$key-1][17] = $_SESSION["bill-v-address"];
$temparray[$key-1][18] = $_SESSION["bill-v-operator"];
$temparray[$key-1][19] = $_SESSION["bill-additional"];
$temparray[$key-1][20] = ""; // placeholder
$temparray[$key-1][21] = ""; // placeholder
$temparray[$key-1][22] = $_SESSION["customername-l"];
$temparray[$key-1][23] = $_SESSION["customername-f"];
$temparray[$key-1][24] = $_SESSION["customeremail"];
$temparray[$key-1][25] = $_SESSION["customerphone"];
$temparray[$key-1][26] = $_SESSION["customeremployer"];
$temparray[$key-1][27] = date('Y-m-d H:i:s');
}
$temparraystr = implode("°§",$temparray[$key-1]); // Why do I need to do this?
$temparray[$key-1] = explode("°§",$temparraystr); // mitä *ttua miksei toimi muuten????
}
// the google sheets part:
// getClient() copypasted from here: https://developers.google.com/sheets/api/quickstart/php
$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'sheet id not shown on stack overflow :)';
$range = "tilaukset!A3:AB";
$valueRange = new Google_Service_Sheets_ValueRange();
$valueRange->setValues($temparray);
$conf = ["valueInputOption" => "RAW"];
$service->spreadsheets_values->append($spreadsheetId, $range, $valueRange, $conf);
如果没有诡计,我会收到这条不太有用的错误消息
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with
message '{\n "error": {\n "code": 400,\n "message": "Invalid JSON
payload received. Unknown name \"1\" at 'data.values': Cannot find
field.\nInvalid JSON payload received. Unknown name \"2\" at
'data.values': Cannot find field.",\n "errors": [\n {\n
"message": "Invalid JSON payload received. Unknown name \"1\"
at 'data.values': Cannot find field.\nInvalid JSON payload received.
Unknown name \"2\" at 'data.values': Cannot find field.",\n
"domain": "global",\n "reason": "badRequest"\n }\n
],\n "status": "INVALID_ARGUMENT"\n }\n}\n'
print_r($temparray) 输出似乎与或不使用内爆-爆炸技巧完全相同。这个:
Array
(
[0] => Array
(
[0] => SfGotLbD-1545531504-HZ2WGmpJ
[1] => evy
[2] => Secondname
[3] => Firstname
[4] => email@host
[5] => employer
[6] => 1
[7] => ph
[8] => ekg
[9] =>
[10] =>
[11] =>
[12] => 1545531504-HZ2WGmpJ
[13] => 1050
[14] =>
[15] => email2@host
[16] => verkkolasku
[17] =>
[18] =>
[19] =>
[20] =>
[21] =>
[22] => Secondname
[23] => Firstname
[24] => email@host
[25] => 555 555555
[26] => employer
[27] => 2018-12-23 04:20:04
)
)
问题是数组中的数据类型错误。它需要是字符串。
数据通过 strval()
以确保它被转换为字符串,这有效。
对于已知为字符串的数据,这不是强制性的。
示例:
$temparray[$key-1][0] = strval($_SESSION["ticketdata"][$key]["ticketid"]);
$temparray[$key-1][1] = strval($_SESSION["tickettype"]);
$temparray[$key-1][2] = strval($_SESSION["ticketdata"][$key]["ticketname-l"]);
$temparray[$key-1][3] = strval($_SESSION["ticketdata"][$key]["ticketname-f"]);
$temparray[$key-1][4] = strval($_SESSION["ticketdata"][$key]["ticketemail"]);
$temparray[$key-1][5] = strval($_SESSION["ticketdata"][$key]["ticketemployer"]);