bhttoan bhttoan - 3 months ago 32
PHP Question

Looping through JSON output from Hubspot deals API

I am trying to use the Hubspot API (http://developers.hubspot.com/docs/overview) to loop through all deals and find only those which are current and then do something with those deals.

No matter what I try to do I cannot get my head around how I access the data I need - below is an example of the output.

In the API there are lots of items like dealstage below and the value field under these is what I need to access - for example in this case the deal is closedlost. Another example would be amount which would also have an entry in value so I can then see the deal value.

I want to loop through all deals and for each deal get the dealstage, amount, last update, owner and so on. Each of these are contained in an array of the same layout as [dealstage] below with a value

I have gotten to where I can print the dealstage value for each deal but it doesn't really help - is there a better way of doing this?

foreach ($list['deals'] as $line) {
foreach ($line['properties'] as $row => $value) {
if ($row=="dealstage") {
$stage=$value['value'];
print $stage."<br>";
}
}
}


Example array:

Array
(
[deals] => Array
(
[0] => Array
(
[portalId] => 12345
[dealId] => 67890
[isDeleted] =>
[associations] => Array
(
[associatedVids] => Array
(
[0] => 4051
)

[associatedCompanyIds] => Array
(
[0] => 23456
)

[associatedDealIds] => Array
(
)

)

[properties] => Array
(
[dealstage] => Array
(
[value] => closedlost
)

[createdate] => Array
(
[value] => 1471334633784
)

[amount] => Array
(
[value] => 1000
)

Answer

Would something like this be what you are looking for. Loop through the array picking out the items you are interested in and place them in a nice simple array for you to use later when building your email.

$for_email = array();

foreach ($list['deals'] as $line) {
    $t = array();
    if (isset($line['properties']['dealstage']['value'])) {
        $t['dealstage'] = $line['properties']['dealstage']['value'];
    }
    if (isset($line['properties']['amount']['value'])) {
        $t['amount'] = $line['properties']['amount']['value'];
    }
    if (isset($line['properties']['createdate']['value'])) {
        $t['createdate'] = $line['properties']['createdate']['value'];
    }

    // any other data you want to capture

    // put this data in the new array
    $for_email[] = $t;
}

// check what the new array looks like
print_r($for_email);
Comments