PHP Multidimensional Array to flat csv -


i'm working on small analytics system fetches information different advertising sources , puts them array. need export information flat csv excel import or other reports software.

here's array

    array (         [site1] => array             (                 [source1] => array                     (                         [campaign1] => array                             (                                 [ad1] => array                                     (                                         [impressions] => 1141379                                         [clicks] => 168                                         [spent] => 113.0382                                     )                              )                          [campaign2] => array                             (                                 [ad2] => array                                     (                                         [impressions] => 612691                                         [clicks] => 89                                         [spent] => 78.0483                                     )                              )                      )              )     [site1] => array         (             [source2] => array                 (                     [campaign1] => array                         (                             [ad1] => array                                 (                                     [impressions] => 877042                                     [clicks] => 87                                     [spent] => 86.13                                 )                          )                      [campaign2] => array                         (                             [ad2] => array                                 (                                     [impressions] => 238749                                     [clicks] => 19                                     [spent] => 18.81                                 )                          )                  )          )     ) 

and csv want have

    site, source, campaign, ad, impressions, clicks, spent     site1, source1, campaign1, ad1, 1141379, 168, 113.0382     site1, source1, campaign2, ad2, 612691, 89, 78.0483     site1, source2, campaign1, ad1, 877042, 87, 86.13     site1, source2, campaign1, ad2, 238749, 19, 18.81 

i can nested foreach cycle process each exact value there better solution recursive function?

instead of writing out values or making recursive functions, consider using fputcsv().

this might solve problem :d.

here example php manual:

<?php  $list = array (     array('aaa', 'bbb', 'ccc', 'dddd'),     array('123', '456', '789'),     array('"aaa"', '"bbb"') );  $fp = fopen('file.csv', 'w');  foreach ($list $fields) {     fputcsv($fp, $fields); }  fclose($fp); ?> 

it output:

aaa,bbb,ccc,dddd 123,456,789 """aaa""","""bbb""" 

hope helps ;)

edit! recursively

function recursive($array) {   $fp = fopen('file.csv', 'w');     foreach($array $arr) {       foreach($arr $a) {          if(is_array($a)) {            return recursive($arr);          } else {            fputcsv($fp, $arr);          }       }     }   fclose($fp); } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -