PHP遍历数组以获取特定值

时间:2020-07-21 19:22:06

标签: php loops

想知道如何从此数组中获取以下值:CampaignID,广告系列,展示次数,点击次数和费用。例如,我想要得到以下结果:

campaignID-9039077962广告系列:CG-智能购物-美国展示-1951940点击次数-10726费用-5324010696

我已经开始使用forloop了,但无法获取任何值,只是得到:ArrayArrayArray

for($i = 0; $i < count($array); $i++) {
    echo $array[2]['row'][$i]['@attributes']['campaignID'];
}

这是我的数组,它是打印结果:

    $array = simplexml_load_string($reportDownloadResult->getAsString());
 

     print_r( $result );
        
    RESULTS------------------------------------------->
    
       Array
    (
        [report-name] => Array
            (
                [@attributes] => Array
                (
                    [name] => CAMPAIGN_PERFORMANCE_REPORT
                )

        )

    [date-range] => Array
        (
            [@attributes] => Array
                (
                    [date] => Jul 13, 2020-Jul 19, 2020
                )

        )

    [table] => Array
        (
            [columns] => Array
                (
                    [column] => Array
                        (
                            [0] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => campaignID
                                            [display] => Campaign ID
                                        )

                                )

                            [1] => Array
                                (
                                    [@attributes] => Array
                                        (
                                            [name] => campaign
                                            [display] => Campaign
                                        )

                                )

                            [2] => A

rray
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => impressions
                                                [display] => Impressions
                                            )
    
                                    )
    
                                [3] => Array
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => clicks
                                                [display] => Clicks
                                            )
    
                                    )
    
                                [4] => Array
                                    (
                                        [@attributes] => Array
                                            (
                                                [name] => cost
                                                [display] => Cost
                                            )
    
                                    )
    
                            )
    
                    )
    
                [row] => Array
                    (
                        [0] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9039077962
                                        [campaign] => CG - Smart Shopping - USA
                                        [impressions] => 1951940
                                        [clicks] => 10726
                                        [cost] => 5324010696
                                    )
    
                            )
    
                        [1] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9040836852
                                        [campaign] => CG - RLSA - USA
                                        [impressions] => 58
                                        [clicks] => 9
                                        [cost] => 3830000
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9335113576
                                        [campaign] => CG - Youtube - Remarketing - US
                                        [impressions] => 11727
                                        [clicks] => 45
                                        [cost] => 470305195
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9336396427
                                        [campaign] => CG - Gmail Display Remarketing - USA
                                        [impressions] => 197
                                        [clicks] => 29
                                        [cost] => 3581214
                                    )
    
                            )
    
                        [4] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9503218446
                                        [campaign] => CG - Shopping - USA - Remarketing
                                        [impressions] => 1169
                                        [clicks] => 13
                                        [cost] => 26480000
                                    )
    
                            )
    
                        [5] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9507461506
                                        [campaign] => CG - Non-branded - Subscription - USA
                                        [impressions] => 6681
                                        [clicks] => 344
                                        [cost] => 813030000
                                    )
    
                            )
    
                        [6] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 9599121241
                                        [campaign] => CG - Display - Remarketing - USA
                                        [impressions] => 13930
                                        [clicks] => 156
                                        [cost] => 21271733
                                    )
    
                            )
    
                        [7] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 10381122443
                                        [campaign] => CG - Branded - USA - Relaunch
                                        [impressions] => 4436
                                        [clicks] => 1113
                                        [cost] => 252800000
                                    )
    
                            )
    
                        [8] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 10438544600
                                        [campaign] => CG - Non-branded Dynamic Shop - USA
                                        [impressions] => 75
                                        [clicks] => 13
                                        [cost] => 13430000
                                    )
    
                            )
    
                        [9] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 10494687070
                                        [campaign] => CG - Non-branded - Shop - USA
                                        [impressions] => 107
                                        [clicks] => 8
                                        [cost] => 11890000
                                    )
    
                            )
    
                        [10] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 10503251956
                                        [campaign] => CG - Gmail Display Remarketing - USA - Box Themes
                                        [impressions] => 13490
                                        [clicks] => 1050
                                        [cost] => 525795143
                                    )
    
                            )
    
                        [11] => Array
                            (
                                [@attributes] => Array
                                    (
                                        [campaignID] => 10505435235
                                        [campaign] => CG - Non-branded - Subscription - CANADA
                                        [impressions] => 1350
                                        [clicks] => 91
                                        [cost] => 131890000
                                    )
    
                            )
    
                    )
    
            )
    
    )

2 个答案:

答案 0 :(得分:0)

最好使用Simple XML的方法来浏览数据,而不是将其视为数组。但是,如果确实需要,我将在下面显示。

我通常建议使用foreach而不是for遍历数组。但是,如果要对数组索引使用for循环,则它们应基于要索引到的同一数组的count()。先使用count($array)然后再使用它来索引到$array[2]['row']是没有道理的-您应该使用count($array[2]['row'])

我不确定您从代码中获得$array[2]的位置。顶级数组没有任何编号索引,它是一个关联数组。在您深入嵌套在内部的rowcolumn元素之前,不会使用编号索引。

foreach ($result['table']['row'] as $row) {
    $attrs = $row['@attributes'];
    echo $attrs['campaignID'];
    echo $attrs['campaign'];
    echo $attrs['impressions'];
    // and so on
}

答案 1 :(得分:0)

#include <memory>

struct Base {
    virtual ~Base() = default;
};
struct Derived : Base {
    void f() const noexcept {}
};

void work(const Base& b) noexcept
{
    // Check what to do, then assume it must be a Derived, then work on it:
    dynamic_cast<const Derived&>(b).f();
}

int main() {
    std::unique_ptr<Base> p{ std::make_unique<Derived>() };
    dynamic_cast<Derived*>(p.get())->f();
    work(*p);

    return 0;
}