2017-07-17 69 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" 
}