匹配值

时间:2016-11-04 09:19:50

标签: php arrays

我有一个从Facebook获取的包含洞察数据的大型动态数组(扩展了一些其他值)

array (size=8)
  0 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033718991' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '102' (length=3)
          'cost_per_inline_link_click' => string '9.224138' (length=8)
          'cpm' => string '23.723653' (length=9)
          'cpp' => string '745.817844' (length=10)
          'ctr' => string '0.301534' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '33827' (length=5)
          'inline_link_clicks' => string '87' (length=2)
          'reach' => string '1076' (length=4)
          'spend' => string '802.5' (length=5)
          'unique_clicks' => string '71' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  1 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033669391' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '940' (length=3)
          'cost_per_inline_link_click' => string '4.21121' (length=7)
          'cpm' => string '18.654177' (length=9)
          'cpp' => string '304.317408' (length=10)
          'ctr' => string '0.530429' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '177215' (length=6)
          'inline_link_clicks' => string '785' (length=3)
          'reach' => string '10863' (length=5)
          'spend' => string '3305.8' (length=6)
          'unique_clicks' => string '585' (length=3)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  2 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053717634591' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '106' (length=3)
          'cost_per_inline_link_click' => string '6.627547' (length=8)
          'cpm' => string '31.457997' (length=9)
          'cpp' => string '61.646192' (length=9)
          'ctr' => string '0.94931' (length=7)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '11166' (length=5)
          'inline_link_clicks' => string '53' (length=2)
          'reach' => string '5698' (length=4)
          'spend' => string '351.26' (length=6)
          'unique_clicks' => string '90' (length=2)
      1 => 
        array (size=7)
          'status' => string 'campaign_paused
' (length=17)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  3 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053404622991' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '19' (length=2)
          'cost_per_inline_link_click' => string '9.9025' (length=6)
          'cpm' => string '31.943548' (length=9)
          'cpp' => string '73.899254' (length=9)
          'ctr' => string '0.766129' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '2480' (length=4)
          'inline_link_clicks' => string '8' (length=1)
          'reach' => string '1072' (length=4)
          'spend' => string '79.22' (length=5)
          'unique_clicks' => string '17' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  4 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053404572991' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '49' (length=2)
          'cost_per_inline_link_click' => string '10.9524' (length=7)
          'cpm' => string '57.067528' (length=9)
          'cpp' => string '88.899351' (length=9)
          'ctr' => string '1.021259' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '4798' (length=4)
          'inline_link_clicks' => string '25' (length=2)
          'reach' => string '3080' (length=4)
          'spend' => string '273.81' (length=6)
          'unique_clicks' => string '34' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  5 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6039438663591' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '1440' (length=4)
          'cost_per_inline_link_click' => string '17.057895' (length=9)
          'cpm' => string '60.323048' (length=9)
          'cpp' => string '345.106481' (length=10)
          'ctr' => string '1.165303' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '123573' (length=6)
          'inline_link_clicks' => string '437' (length=3)
          'reach' => string '21600' (length=5)
          'spend' => string '7454.3' (length=6)
          'unique_clicks' => string '1010' (length=4)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  6 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6039438653791' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '332' (length=3)
          'cost_per_inline_link_click' => string '14.831231' (length=9)
          'cpm' => string '60.289556' (length=9)
          'cpp' => string '206.563103' (length=10)
          'ctr' => string '1.038149' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '31980' (length=5)
          'inline_link_clicks' => string '130' (length=3)
          'reach' => string '9334' (length=4)
          'spend' => string '1928.06' (length=7)
          'unique_clicks' => string '245' (length=3)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  7 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6038268312591' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '112' (length=3)
          'cost_per_inline_link_click' => string '5.063291' (length=8)
          'cpm' => string '41.968314' (length=9)
          'cpp' => string '81.984013' (length=9)
          'ctr' => string '1.175113' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '9531' (length=4)
          'inline_link_clicks' => string '79' (length=2)
          'reach' => string '4879' (length=4)
          'spend' => string '400' (length=3)
          'unique_clicks' => string '96' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
C:\Development\Projects\PHP\SparNord.LeadsData\SparNord.LeadsData\app\Controllers\InsightsDataController.php:78:
array (size=8)
  0 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033718991' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '102' (length=3)
          'cost_per_inline_link_click' => string '9.224138' (length=8)
          'cpm' => string '23.723653' (length=9)
          'cpp' => string '745.817844' (length=10)
          'ctr' => string '0.301534' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '33827' (length=5)
          'inline_link_clicks' => string '87' (length=2)
          'reach' => string '1076' (length=4)
          'spend' => string '802.5' (length=5)
          'unique_clicks' => string '71' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  1 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033669391' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '940' (length=3)
          'cost_per_inline_link_click' => string '4.21121' (length=7)
          'cpm' => string '18.654177' (length=9)
          'cpp' => string '304.317408' (length=10)
          'ctr' => string '0.530429' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '177215' (length=6)
          'inline_link_clicks' => string '785' (length=3)
          'reach' => string '10863' (length=5)
          'spend' => string '3305.8' (length=6)
          'unique_clicks' => string '585' (length=3)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  2 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053717634591' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '106' (length=3)
          'cost_per_inline_link_click' => string '6.627547' (length=8)
          'cpm' => string '31.457997' (length=9)
          'cpp' => string '61.646192' (length=9)
          'ctr' => string '0.94931' (length=7)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '11166' (length=5)
          'inline_link_clicks' => string '53' (length=2)
          'reach' => string '5698' (length=4)
          'spend' => string '351.26' (length=6)
          'unique_clicks' => string '90' (length=2)
      1 => 
        array (size=7)
          'status' => string 'campaign_paused
' (length=17)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  3 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053404622991' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '19' (length=2)
          'cost_per_inline_link_click' => string '9.9025' (length=6)
          'cpm' => string '31.943548' (length=9)
          'cpp' => string '73.899254' (length=9)
          'ctr' => string '0.766129' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '2480' (length=4)
          'inline_link_clicks' => string '8' (length=1)
          'reach' => string '1072' (length=4)
          'spend' => string '79.22' (length=5)
          'unique_clicks' => string '17' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  4 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6053404572991' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '49' (length=2)
          'cost_per_inline_link_click' => string '10.9524' (length=7)
          'cpm' => string '57.067528' (length=9)
          'cpp' => string '88.899351' (length=9)
          'ctr' => string '1.021259' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '4798' (length=4)
          'inline_link_clicks' => string '25' (length=2)
          'reach' => string '3080' (length=4)
          'spend' => string '273.81' (length=6)
          'unique_clicks' => string '34' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  5 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6039438663591' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '1440' (length=4)
          'cost_per_inline_link_click' => string '17.057895' (length=9)
          'cpm' => string '60.323048' (length=9)
          'cpp' => string '345.106481' (length=10)
          'ctr' => string '1.165303' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '123573' (length=6)
          'inline_link_clicks' => string '437' (length=3)
          'reach' => string '21600' (length=5)
          'spend' => string '7454.3' (length=6)
          'unique_clicks' => string '1010' (length=4)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  6 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6039438653791' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '332' (length=3)
          'cost_per_inline_link_click' => string '14.831231' (length=9)
          'cpm' => string '60.289556' (length=9)
          'cpp' => string '206.563103' (length=10)
          'ctr' => string '1.038149' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '31980' (length=5)
          'inline_link_clicks' => string '130' (length=3)
          'reach' => string '9334' (length=4)
          'spend' => string '1928.06' (length=7)
          'unique_clicks' => string '245' (length=3)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  7 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6038268312591' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6037440286791' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '112' (length=3)
          'cost_per_inline_link_click' => string '5.063291' (length=8)
          'cpm' => string '41.968314' (length=9)
          'cpp' => string '81.984013' (length=9)
          'ctr' => string '1.175113' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '9531' (length=4)
          'inline_link_clicks' => string '79' (length=2)
          'reach' => string '4879' (length=4)
          'spend' => string '400' (length=3)
          'unique_clicks' => string '96' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)

如何在密钥'shortname'相同的特定密钥(点击次数,cpm,ctr,展示次数,覆盖面,支出)上合并这些数组和总和值,以便我只获得一个包含每个短名称的数组,将spefific键上的值相加,这样我的结果就像

array (size=8)
  0 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033718991' (length=13)
          'adset_name' => string 'Greve - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '102' (length=3)
          'cost_per_inline_link_click' => string '9.224138' (length=8)
          'cpm' => string '23.723653' (length=9)
          'cpp' => string '745.817844' (length=10)
          'ctr' => string '0.301534' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '33827' (length=5)
          'inline_link_clicks' => string '87' (length=2)
          'reach' => string '1076' (length=4)
          'spend' => string '802.5' (length=5)
          'unique_clicks' => string '71' (length=2)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'greve' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)
  1 => 
    array (size=2)
      0 => 
        array (size=16)
          'adset_id' => string '6054033669391' (length=13)
          'adset_name' => string 'Køge - MSN' (length=11)
          'campaign_id' => string '6054033668391' (length=13)
          'campaign_name' => string 'Køge - MSN' (length=11)
          'clicks' => string '940' (length=3)
          'cost_per_inline_link_click' => string '4.21121' (length=7)
          'cpm' => string '18.654177' (length=9)
          'cpp' => string '304.317408' (length=10)
          'ctr' => string '0.530429' (length=8)
          'date_start' => string '2016-01-01' (length=10)
          'date_stop' => string '2016-10-25' (length=10)
          'impressions' => string '177215' (length=6)
          'inline_link_clicks' => string '785' (length=3)
          'reach' => string '10863' (length=5)
          'spend' => string '3305.8' (length=6)
          'unique_clicks' => string '585' (length=3)
      1 => 
        array (size=7)
          'status' => string 'paused
' (length=8)
          'bankarea' => string 'koege' (length=5)
          'shortname' => string 'koege' (length=5)
          'campaign' => string 'msn' (length=3)
          'platform' => string 'facebook' (length=8)
          'datefrom' => string '2016-01-01' (length=10)
          'dateto' => string '2016-10-25' (length=10)

将键clicks, cpm, ctr, impressions, reach, spend上的值相加(上面的示例中没有)

1 个答案:

答案 0 :(得分:1)

这应该做你想要的:

$props_to_sum = ["cpm", "clicks", "reach"];

$result = [];
foreach ($data as $entry) {
    $key = $entry[1]['shortname'];
    if (!array_key_exists($key, $result)) {
        $result[$key] = $entry;
        continue;
    }
    foreach ($props_to_sum as $prop) {
        $result[$key][0][$prop] += $entry[0][$prop];
    }
}
$result = array_values($result);