PHP 遍历嵌套 JSON 响应并重组为 Webhook 的简单查询字符串
PHP Loop Through Nested JSON Response and Reassemble as Simple Query String for Webhook
我需要将数据发送到 Zapier webhook,因此,他们更喜欢使用简单的查询字符串。
如何使用 PHP 循环访问以下 JSON 响应以将其重新组合为单个对象?我需要将它作为一个简单的查询字符串发送到我的 Zapier webhook。
具体来说,我真的想在 Zapier 标签输入字段中使用 "tags" 和 "tech" 数据,这意味着我必须将它们作为单独的选项发送,否则所有 "tags" 和 "tech" 数据作为单个标签发送,而不是按照您希望它们出现的方式发送单个标签。
正如您将在下面看到的,一些值具有数组,而其他值具有 key/value 对。
{
"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",
}
此代码适用于您的方案。但是,如果您需要一个适用于多个嵌套数组的通用函数,我们需要编写一个递归函数。
<?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
)
我需要将数据发送到 Zapier webhook,因此,他们更喜欢使用简单的查询字符串。
如何使用 PHP 循环访问以下 JSON 响应以将其重新组合为单个对象?我需要将它作为一个简单的查询字符串发送到我的 Zapier webhook。
具体来说,我真的想在 Zapier 标签输入字段中使用 "tags" 和 "tech" 数据,这意味着我必须将它们作为单独的选项发送,否则所有 "tags" 和 "tech" 数据作为单个标签发送,而不是按照您希望它们出现的方式发送单个标签。
正如您将在下面看到的,一些值具有数组,而其他值具有 key/value 对。
{
"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",
}
此代码适用于您的方案。但是,如果您需要一个适用于多个嵌套数组的通用函数,我们需要编写一个递归函数。
<?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
)