PHP 循环遍历嵌套的 JSON 响应并重新组装为 Webhook 的简单查询字符串
Posted
技术标签:
【中文标题】PHP 循环遍历嵌套的 JSON 响应并重新组装为 Webhook 的简单查询字符串【英文标题】:PHP Loop Through Nested JSON Response and Reassemble as Simple Query String for Webhook 【发布时间】:2017-11-18 15:02:34 【问题描述】:我需要将数据发送到 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",
【问题讨论】:
【参考方案1】:此代码适用于您的方案。但是,如果您需要一个适用于多个嵌套数组的泛型函数,我们需要编写一个递归函数。
<?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
)
【讨论】:
以上是关于PHP 循环遍历嵌套的 JSON 响应并重新组装为 Webhook 的简单查询字符串的主要内容,如果未能解决你的问题,请参考以下文章