2017-06-16 1 views
0

Ich muss Daten an einen Zapier Webhook senden, als Ergebnis bevorzugen sie einfache Abfrage Zeichenfolgen.PHP-Schleife durch verschachtelte JSON-Antwort und Reassemblieren als einfache Abfrage String für Webhook

Wie kann ich PHP verwenden, um durch die folgende JSON-Antwort durchzulaufen, um es als einzelnes Objekt wieder zusammenzusetzen? Ich muss es als eine einfache Abfrage Zeichenfolge an meine Zapier Webhook senden.

Insbesondere möchte ich "Tags" und "Tech" -Daten im Eingabefeld Zapier-Tags verwenden, was bedeutet, dass ich sie als individuelle Optionen senden muss, sonst werden alle "Tags" und "Tech" -Daten erhalten gesendet als einzelnes Tag, anstatt einzelne Tags so wie Sie möchten, dass sie angezeigt werden.

Wie Sie unten sehen werden, haben einige Werte Arrays, während andere Schlüssel/Wert-Paare haben.

{ 
    "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",   
    ] 
    } 
} 

ich es aussehen soll ist die folgende (beachten Sie die _0x Zahlen gegebenenfalls angehängt wird.

{ 
    "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", 
} 

Antwort

1

Dieser Code für Ihr Szenario gut funktioniert. Aber wenn Sie eine generische Funktion, dass arbeitet mit mehreren verschachtelten Arrays, müssen wir eine rekursive Funktion schreiben

<?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" 
    ] 
    } 
} 

Ausgang:

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 
) 
Verwandte Themen