Page 1 of 1

[SOLVED] explode () woes

Posted: Mon Oct 23, 2006 10:36 am
by malcolmboston
ok im baffled to be honest

heres some information in a (CSV) text file

Code: Select all

Array
(
    [1] => Day,Date,Page Loads,Unique Visitors,First Time Visitors,Returning Visitors
    [2] => Saturday,1,"98","9","5","4"
    [3] => Sunday,2,"170","72","66","6"
    [4] => Monday,3,"448","100","89","11"
    [5] => Tuesday,4,"472","179","173","6"
    [6] => Wednesday,5,"593","133","126","7"
    [7] => Thursday,6,"605","131","128","3"
    [8] => Friday,7,"370","82","69","13"
    [9] => Saturday,8,"296","40","34","6"
    [10] => Sunday,9,"323","53","47","6"
    [11] => Monday,10,"273","87","80","7"
    [12] => Tuesday,11,"272","33","26","7"
    [13] => Wednesday,12,"223","42","33","9"
    [14] => Thursday,13,"483","67","61","6"
    [15] => Friday,14,"325","70","63","7"
    [16] => Saturday,15,"208","18","12","6"
    [17] => Sunday,16,"89","36","28","8"
    [18] => Monday,17,"220","64","55","9"
    [19] => Tuesday,18,"410","50","39","11"
    [20] => Wednesday,19,"362","53","46","7"
    [21] => Thursday,20,"196","33","23","10"
    [22] => Friday,21,"480","42","34","8"
    [23] => Saturday,22,"300","136","130","6"
    [24] => Sunday,23,"302","44","40","4"
    [25] => Monday,24,"478","194","187","7"
    [26] => Tuesday,25,"433","115","105","10"
    [27] => Wednesday,26,"320","132","125","7"
    [28] => Thursday,27,"830","321","313","8"
    [29] => Friday,28,"327","148","144","4"
    [30] => Saturday,29,"257","57","52","5"
    [31] => Sunday,30,"283","50","44","6"
    [32] => Monday,31,"334","78","71","7"
    [33] => 
)
now heres the code im using, its sloppy at the moment :(

Code: Select all

<?php
error_reporting (E_ALL);
require ('includes/functions.php');
require ('includes/jpgraph/src/jpgraph.php');
require ('includes/jpgraph/src/jpgraph_bar.php');
// we need to collect the data
?>

<?php
$array = readMyFile ("C:/Documents and Settings/redsquare/Desktop/site_logs/eStore/july_06.csv");
print "<pre>";
print_r($array);
print "</pre>";
for ($i = 2; $i < count ($array); $i++) {
   if (empty($array[$i])) {
      unset ($array[$i]);
   } else {
      if (!isset ($j)) {
         $j = 1;
      }
      // exlode
      $explosion = explode (",", trim($array[$i]));
      /*
      [0] Day (Saturday)
      [1] Date (1st July 2006)
      [2] Page Loads (98)
      [3] Unique Visitors (28)
      [4] First Time Visitors (736)
      [5] Returning Visitors (23)
      */
// heres the troublesome area
      $arrayNew[$j]['date'] = $array[$i][1];
      $arrayNew[$j]['page_loads'] = $array[$i][2];
      $arrayNew[$j]['unique'] = $array[$i][3];
      $arrayNew[$j]['first_time'] = $array[$i][4];
      $arrayNew[$j]['returning'] = $array[$i][5];
      $j++;
   }
}

print "<pre>";
print_r ($arrayNew);
print "</pre>";
?>
now instead of getting what im expecting in arrayNew im getting the following

Code: Select all

Array
(
    [1] => Array
        (
            [date] => a
            [page_loads] => t
            [unique] => u
            [first_time] => r
            [returning] => d
        )

    [2] => Array
        (
            [date] => u
            [page_loads] => n
            [unique] => d
            [first_time] => a
            [returning] => y
        )

    [3] => Array
        (
            [date] => o
            [page_loads] => n
            [unique] => d
            [first_time] => a
            [returning] => y
        )
can someone please point me in the right direction, im baffled by this :(

Posted: Mon Oct 23, 2006 10:42 am
by volka
You might want to use fgetcsv instead
see http://de2.php.net/fgetcsv

Posted: Mon Oct 23, 2006 10:57 am
by malcolmboston
nevermind, solved utilising fgetcsv

thanks