2017-07-17 67 views
-4

在PHP中,我怎麼能做出這樣的數據:使用電子郵件設置一個扎普(通過Zapier):顯示鍵/值

{ 
    "items": { 
    "item": [{ 
      "id": "59", 
      "type": "Domain", 
      "relid": "27", 
      "description": "Sample Monthly Product(01\/01\/2016 - 31\/01\/2016)", 
      "amount": "180.00", 
      "taxed": "0" 
     }] 
    } 
} 

具有以下格式:

{ 
    "items[item][0][id]": "59", 
    "items[item][0][type]": "Domain", 
    "items[item][0][relid]": "27", 
    "items[item][0][description]": "Sample Monthly Product (01\/01\/2016 - 31\/01\/2016)", 
    "items[item][0][amount]": "180.00", 
    "items[item][0][taxed]": "0" 
} 

原因解析器從WHMCS進口發票,這似乎與解析數據進行工作時,第二格式(項目[項目] [0] [ID])來讀取行的描述,但使用1號的時候沒有。在WHMCS API ref中顯示它輸出爲第二種格式,但看不出爲什麼我的看起來像1st(developers.whmcs.com/api-reference/getinvoice)

+0

你真的想你的關鍵是:' 「項目[項目] [0] [ID]」'?這很奇怪。 –

+0

你爲什麼想要。這幾乎已經是你想要什麼反正 – RiggsFolly

+0

歡迎的StackOverflow!首先,你有一個「對象」,而不是「數組」。其次,你的對象充滿了語法錯誤。你能確保信息是正確的嗎?第三,**你爲什麼要嘗試像這樣重組?您的數據已經處於高度可訪問的格式。你只是想添加一個'[0]'索引?假設你在循環中運行它,你可以使用'foreach'。 –

回答

0

我認爲這可能適用於您當前的設置:

<?php 

//assuming your current dataset isn't in JSON, you can ignore this part 
$json = '{ 
    "items": { 
    "item": [{ 
      "id": "59", 
      "type": "Domain", 
      "relid": "27", 
      "description": "Sample Monthly Product", 
      "amount": "180.00", 
      "taxed": "0" 
     }, 
     { 
      "id": "203", 
      "type": "Server", 
      "relid": "86", 
      "description": "Sample Yearly Product", 
      "amount": "290.00", 
      "taxed": "1" 
     }] 
    } 
}'; 

$json = json_decode($json, true); 

$parsed = array(); 

foreach ($json['items']['item'] as $index => $item) 
    foreach ($item as $attr => $val) 
     $parsed['items[item][' . $index . '][' . $attr . ']'] = $val; 

echo json_encode($parsed); 

Output

{ 
    "items[item][0][id]": "59", 
    "items[item][0][type]": "Domain", 
    "items[item][0][relid]": "27", 
    "items[item][0][description]": "Sample Monthly Product", 
    "items[item][0][amount]": "180.00", 
    "items[item][0][taxed]": "0", 
    "items[item][1][id]": "203", 
    "items[item][1][type]": "Server", 
    "items[item][1][relid]": "86", 
    "items[item][1][description]": "Sample Yearly Product", 
    "items[item][1][amount]": "290.00", 
    "items[item][1][taxed]": "1" 
}