我需要将数据发送到Zapier webhook,因此,他们更喜欢使用简单的查询字符串。
如何使用PHP循环通过以下JSON响应将其重新组合为单个对象?我需要将它作为一个简单的查询字符串发送到我的Zapier webhook。
具体来说,我真的想在Zapier标签输入字段中使用“标签”和“技术”数据,这意味着我必须将它们作为单独的选项发送,否则所有“标签”和“技术”数据都会被发送作为单个标记,而不是单个标记,就像您希望它们出现一样。
正如您将在下面看到的,某些值具有数组,而其他值具有键/值对。
{
"company": {
"name": "Example Co",
"legalName": "Example Co LLC",
"domain": "example.com",
"domainAliases": [
"example01.com",
"example02.com",
"example03.com"
],
"category": {
"sector": "Financials",
"industryGroup": "Diversified Financial Services",
"industry": "Diversified Financial Services",
"subIndustry": "Financial Services",
},
"tags": [
"Marketplace",
"B2C",
"Financial Services"
],
"foundedYear": null,
"tech": [
"google_analytics",
"hotjar",
"outlook",
]
}
}
我希望它看起来如下(注意在适用的地方附加_0x号码。
{
"company_name": "Example Co",
"company_legalName": "Example Co LLC",
"company_domain": "example.com",
"company_domainAliases_01": "example01.com",
"company_domainAliases_02": "example02.com",
"company_domainAliases_03": "example03.com",
"category_sector": "Financials",
"category_industryGroup": "Diversified Financial Services",
"category_industry": "Diversified Financial Services",
"category_subIndustry": "Financial Services",
"category_sicCode": null,
"category_naicsCode": null,
"tags": "Marketplace",
"tags": "B2C",
"tags": "Financial Services",
"foundedYear": null,
"tech_01": "google_analytics",
"tech_02": "hotjar",
"tech_03": "outlook",
}
答案 0 :(得分:2)
此代码适用于您的方案。但是如果你需要一个泛型函数,它适用于多个嵌套数组,我们需要编写一个递归函数。
<?php
$json_contents = file_get_contents('test.json'); //Placed your json in test.json
$output = array();
$json_array = json_decode($json_contents, true); //Convert json to php array
foreach ($json_array as $element){
foreach ($element as $attribute => $value){ //parse through each attribute
if(is_array($value)){ //if value is an array, parse through it and update output array accordingly
foreach ($value as $a => $v) {
$output[$attribute.'_'.$a] = $v;
}
}
else{ //if value is not an array, get those elements as they are into $output
$output[$attribute] = $value;
}
}
}
print_r($output); //You can convert $output into json using json_encode($output);
print_r($json_array);
test.json:
{
"company": {
"name": "Example Co",
"legalName": "Example Co LLC",
"domain": "example.com",
"domainAliases": [
"example01.com",
"example02.com",
"example03.com"
],
"category": {
"sector": "Financials",
"industryGroup": "Diversified Financial Services",
"industry": "Diversified Financial Services",
"subIndustry": "Financial Services"
},
"tags": [
"Marketplace",
"B2C",
"Financial Services"
],
"foundedYear": null,
"tech": [
"google_analytics",
"hotjar",
"outlook"
]
}
}
输出:
Array
(
[name] => Example Co
[legalName] => Example Co LLC
[domain] => example.com
[domainAliases_0] => example01.com
[domainAliases_1] => example02.com
[domainAliases_2] => example03.com
[category_sector] => Financials
[category_industryGroup] => Diversified Financial Services
[category_industry] => Diversified Financial Services
[category_subIndustry] => Financial Services
[tags_0] => Marketplace
[tags_1] => B2C
[tags_2] => Financial Services
[foundedYear] =>
[tech_0] => google_analytics
[tech_1] => hotjar
[tech_2] => outlook
)