dpd66100 2017-01-10 03:31
浏览 44

按键的多维数组的和值

I have this multi dimensional array

array(192) {
   [0]=>
   object(stdClass)#29 (16) {
   ["campaignId"]=>
     int(746598)
   ["creativeId"]=>
     int(11062415)
   ["targetId"]=>
     int(814283)
   ["targetStatus"]=>
     string(6) "ACTIVE"
   ["impressions"]=>
     int(9)
   ["clicks"]=>
     int(0)
   ["cost"]=>
     float(0.3003)
   ["ctr"]=>
     NULL
   ["cpm"]=>
     float(33.3667)
   ["conversions"]=>
     int(2)
   ["revenue"]=>
     float(4)
   ["cpa"]=>
     float(0.1502)
   ["cr"]=>
     float(22.22222)
   ["averageRank"]=>
     float(1.9)
   ["maxBid"]=>
     float(0.884)
   ["averageBid"]=>
     float(0.0334)
   }
[1]=>
   object(stdClass)#29 (16) {
   ["campaignId"]=>
     int(746598)
   ["creativeId"]=>
     int(11062415)
   ["targetId"]=>
     int(814283)
   ["targetStatus"]=>
     string(6) "ACTIVE"
   ["impressions"]=>
     int(9)
   ["clicks"]=>
     int(0)
   ["cost"]=>
     float(0.3003)
   ["ctr"]=>
     NULL
   ["cpm"]=>
     float(33.3667)
   ["conversions"]=>
     int(2)
   ["revenue"]=>
     float(4)
   ["cpa"]=>
     float(0.1502)
   ["cr"]=>
     float(22.22222)
   ["averageRank"]=>
     float(1.9)
   ["maxBid"]=>
     float(0.884)
   ["averageBid"]=>
     float(0.0334)
   }
[2]=>
    object(stdClass)#29 (16) {
    ["campaignId"]=>
      int(746595)
    ["creativeId"]=>
      int(11062415)
    ["targetId"]=>
      int(814283)
    ["targetStatus"]=>
      string(6) "ACTIVE"
    ["impressions"]=>
       int(9)
    ["clicks"]=>
      int(0)
    ["cost"]=>
      float(0.3003)
    ["ctr"]=>
      NULL
    ["cpm"]=>
      float(33.3667)
    ["conversions"]=>
      int(2)
    ["revenue"]=>
      float(4)
    ["cpa"]=>
     float(0.1502)
    ["cr"]=>
      float(22.22222)
    ["averageRank"]=>
      float(1.9)
    ["maxBid"]=>
      float(0.884)
    ["averageBid"]=>
      float(0.0334)
   }
  [3]=>
object(stdClass)#29 (16) {
   ["campaignId"]=>
     int(746595)
    ["creativeId"]=>
      int(11062415)
    ["targetId"]=>
     int(814283)
   ["targetStatus"]=>
      string(6) "ACTIVE"
   ["impressions"]=>
      int(9)
   ["clicks"]=>
     int(0)
   ["cost"]=>
     float(0.3003)
   ["ctr"]=>
     NULL
   ["cpm"]=>
    float(33.3667)
   ["conversions"]=>
     int(2)
   ["revenue"]=>
     float(4)
   ["cpa"]=>
     float(0.1502)
   ["cr"]=>
     float(22.22222)
   ["averageRank"]=>
     float(1.9)
   ["maxBid"]=>
     float(0.884)
   ["averageBid"]=>
     float(0.0334)
   }
 }
}

I want to sum the values of cost and get the average of cpm every campaign id and I only get the last part of the array.

This is my code

while (strtotime($startDate) <= strtotime($endDate)) {
    $newStartDate = date ("Y-m-d", strtotime("+1 day", strtotime($startDate)));

    $params = array(
        'date'=> $newStartDate
    );
    $reports = $client->__soapCall('getDailyTargetsStats', array($params));

    $dateToday = date('Y-m-d');
    $cost = 0;
    $resultArray = $reports->stats->item;
    foreach ($resultArray as $value) {
        $campaignId = $value->campaignId;
        $cost += $value->cost;

        if ((!$newStartDate) || (!$campaignId)) {
            continue;
        }
        //get campaign names
        $campaignName =  $this->get_campaigns($campaignId);

        $data = array(
            'date' => $newStartDate,
            'campaign_id' => $campaignId,
            'campaign_name' => $campaignName,
            'cost' => $cost
        );
        echo '<pre>';
        var_dump($data);
        echo '</pre>';
        exit;   
    }
    sleep(5);
    $startDate = $newStartDate;
}

Any suggestions will do, I am currently developing it in CodeIgniter and PHP 5.6.

Then current code now is this

foreach ($resultArray as $value) {
                $campaignId = $value->campaignId;
                $cost += $value->cost;

                if ((!$newStartDate) || (!$campaignId)) {
                    continue;
                }

                //get campaign names
                $campaignName =  $this->get_campaigns($campaignId);

                $data = array(
                    'date' => $newStartDate,
                    'campaign_id' => $campaignId,
                    'campaign_name' => $campaignName,
                    'cost' => $cost
                );



            }
            echo '<pre>';
            var_dump($data);
            echo '</pre>';
            exit;
  • 写回答

1条回答 默认 最新

  • douxuan1284 2017-01-10 05:49
    关注

    This may work for you:

    foreach ($resultArray as $value) {
        $campaignId = $value->campaignId;
        $newStartDate = $value->newStartDate;
    
        if ((!$newStartDate) || (!$campaignId)) {
            continue;
        }
    
        $cost = $value->cost;
    
        //get campaign names
        $campaignName = $this->get_campaigns($campaignId);
    
        $data[campaign_id] = array(
            'date' => $newStartDate,
            'campaign_id' => $campaignId,
            'campaign_name' => $campaignName,
            'cost' => $data['campaign_name'] + $cost
        );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂