【问题标题】:Looping through JSON output from Hubspot deals API循环通过 Hubspot 交易 API 的 JSON 输出
【发布时间】:2016-12-22 13:06:14
【问题描述】:

我正在尝试使用 Hubspot API (http://developers.hubspot.com/docs/overview) 循环浏览所有交易并仅查找当前的交易,然后对这些交易进行处理。

无论我尝试做什么,我都无法理解如何访问所需的数据 - 下面是输出示例。

在 API 中有很多项目,例如下面的 dealstage,这些项目下的 value 字段是我需要访问的 - 例如在这种情况下,交易已关闭。另一个例子是金额,它也有一个值条目,所以我可以看到交易价值。

我想遍历所有交易,并为每笔交易获取交易阶段、金额、上次更新、所有者等。这些中的每一个都包含在一个与下面的 [dealstage] 具有相同布局的数组中,并带有一个值

我已经到了可以打印每笔交易的交易阶段价值的地方,但它并没有真正帮助 - 有没有更好的方法来做到这一点?

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

示例数组:

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
                                )

【问题讨论】:

  • 您只是想打印这些数据还是想捕获它以供稍后在代码中使用
  • 我希望以后能够使用它 - 我的想法是在我浏览完所有交易后发送一封电子邮件,其中包含 x 天内未更新的所有交易

标签: php arrays loops multidimensional-array foreach


【解决方案1】:

您是否正在寻找这样的东西。循环遍历数组,挑选出您感兴趣的项目,并将它们放在一个漂亮的简单数组中,供您稍后在构建电子邮件时使用。

$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);

【讨论】:

  • 是的,这行得通 - 您的代码中几乎没有错别字。 for_email=array() 应该是 $for_email 和 if(isset are each missing a closing ) 在开始 {.感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2021-11-30
  • 2021-06-21
  • 1970-01-01
  • 2023-01-14
  • 2023-03-20
  • 2021-06-25
相关资源
最近更新 更多