note 83799 added to function.array-keys

From: Date: Thu, 12 Jun 2008 12:46:26 +0000
Subject: note 83799 added to function.array-keys
Groups: php.notes 
Request: Send a blank email to php-notes+get-140374@lists.php.net to get a copy of this message
/* * *This function will return a .csv from a given array inside the $_SESSION['my_array'] * *$csv_name -> the name we want the csv has to *$download -> true or false to download the csv file after done * */ <?php function createCSV($csv_name, $download) { $i = 1; $csv = ""; /* erase the old file, if it exists */ @unlink("../../csv/" . $csv_name . ".csv"); /* array is in a session variable * this may be useful to avoid many db queries if it is the case */ $my_array = $_SESSION['my_array']; /* how many fields has the given array */ $fields = count(array_keys($my_array[0])); /* extracting the titles from the array */ foreach(array_keys($my_array[0]) as $title) { /* array_keys percurs the title of each vector */ $csv .= $title; /* while it is not the last field put a semi-colon ; */ if($i < $fields) $csv .= ";"; $i++; } /* insert an empty line to better visualize the csv */ $csv .= chr(10).chr(13); $csv .= chr(10).chr(13); /* get the values from the extracted keys */ foreach (array_keys($my_array) as $tipo) { $i = 1; foreach(array_keys($my_array[$tipo]) as $sub) { $csv .= $my_array[$tipo][$sub]; if ($i < $fields) $csv .= ";"; $i++; } $csv .= chr(10).chr(13); } /* export the csv */ $export_csv=fopen("../../csv/". $csv_name .".csv", "w+"); fwrite($export_csv, $csv); fclose($export_csv); /* download the csv */ if ($download == true) header('Location:' . "../../csv/" . $csv_name . ".csv"); exit(); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 201.6.108.30 ---- Manual Page -- http://www.php.net/manual/en/function.array-keys.php Edit -- https://master.php.net/note/edit/83799 Del: integrated -- https://master.php.net/note/delete/83799/integrated Del: useless -- https://master.php.net/note/delete/83799/useless Del: bad code -- https://master.php.net/note/delete/83799/bad+code Del: spam -- https://master.php.net/note/delete/83799/spam Del: non-english -- https://master.php.net/note/delete/83799/non-english Del: in docs -- https://master.php.net/note/delete/83799/in+docs Del: other reasons-- https://master.php.net/note/delete/83799 Reject -- https://master.php.net/note/reject/83799 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#140374) next »