1
我正在使用XMLRPC構建將產品信息傳遞到第三方系統的XML結構。我需要構建產品自定義選項的關聯數組,並且我不知道使用什麼語法,因爲每種情況下的值都是對象。使用foreach以對象作爲值創建關聯數組
我無法調試和玩它,因爲我通常會相信與否我必須在現場做這個,所以我一直在給自己發送電子郵件,以確保它看起來沒問題,那麼當我將它應用到網站時,XMLRPC會拋出一個錯誤,說它無法序列化我構建的對象,然後我很快將它切換回來。
如果我像這樣硬編碼它工作正常。
$item_array = array(
"product_id" => new xmlrpcval($item->getSku()),
"name" => new xmlrpcval($item->getName()),
"price" => new xmlrpcval($item->getBaseCalculationPrice(), 'double'),
"vat_inclusive" => new xmlrpcval(0,'int'),
"quantity" => new xmlrpcval($item->getQty(),'int'),
"option_text" => new xmlrpcval(
array(
"option_1" => new xmlrpcval("Colour: Military Black"),
"option_2" => new xmlrpcval("Sizes: L")
),
"struct")
);
這是如下因素部分我需要生成,特別是在foreach循環中,因爲我不知道有多少的選項會有數組;
"option_text" => new xmlrpcval(
array(
"option_1" => new xmlrpcval("Colour: Military Black"),
"option_2" => new xmlrpcval("Sizes: L")
),
"struct")
如果我不喜歡它下面再談到了罰款,但價值是一個字符串,而不是一個對象,XML-RPC不能序列;
$optioncount = 1;
$attList = array();
foreach ($attributes as $attribute => $value) {
$attpair = implode(": ", $value);
$attList['option_'. $optioncount] = 'new xmlrpcval("'.$attpair.'")';
$optioncount++;
}
If I var_dump($attList)
I get;
array(2) {
["option_1"]=>
string(39) "new xmlrpcval("Colour: Military Black")"
["option_2"]=>
string(25) "new xmlrpcval("Sizes: L")"
}
任何其他方式似乎把$attList
成一團糟 - 我知道這應該是很基本的,但對於生活我的,我不能得到這個工作。感謝任何指針。
如果我var_dump($attList)
當我用new xmlrpcval($attpair);
我得到;
array(2) {
["option_1"]=>
object(xmlrpcval)#469 (3) {
["me"]=>
array(1) {
["string"]=>
string(22) "Colour: Military Black"
}
["mytype"]=>
int(1)
["_php_class"]=>
NULL
}
["option_2"]=>
object(xmlrpcval)#433 (3) {
["me"]=>
array(1) {
["string"]=>
string(8) "Sizes: L"
}
["mytype"]=>
int(1)
["_php_class"]=>
NULL
}
}
你肯定繞'新xmlrpcval引號(「」 $。 attpair。'「)'?這似乎很奇怪...不應該只是'new xmlrpcval($ attpair)'? – tmuguet
當沒有硬編碼時,剩下的代碼是什麼? – tmuguet
@tmuguet - 讓我們來看看。如果我這樣做,我編輯了答案以顯示我得到的是$ attList。 – McNab