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;