Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8000
[8] => 6000
)
[2] => Array
(
[0] =>
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Multi-Touch Email Campaign
[7] => 350
[8] => 200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9000
[8] => 4000
)
[4] => Array
(
[0] =>
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Linkedin(Groups)
[7] => 350
[8] => 200
)
)
)
您好,我是PHP新手。 我只想在这个数组中我想添加特定的数组键值,并确定在确定所有特定值的值应添加到新数组后,它应该具有相同的伙伴和divison。
答案应该是这样的:
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[2] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
)
答案 0 :(得分:2)
试试这段代码,这绝对适合你..
$removeKeys = array();
foreach($data['csv_data'] as $key => $val)///loop through array..
{
foreach($data['csv_data'] as $k => $v)
{
if($val[1] == $v[1] && $key != $k)////check if key 1 matches
{
if(!in_array($key,$removeKeys)) ////check if item is already added or not
{
$removeKeys[] = $k; ///push into removed keys because this is added into matched item
$data['csv_data'][$key][7]+=$data['csv_data'][$k][7];
$data['csv_data'][$key][8]+=$data['csv_data'][$k][8];
}
}
}
}
foreach($removeKeys as $rk)
{
unset($data['csv_data'][$rk]); ////remove all the keys in removeKeys
}
print_r($data['csv_data']);///your desired output...
这会给你:
Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
现场示例:CLICK HERE