Appending csv data to an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilbit02
    New Member
    • Nov 2007
    • 22

    Appending csv data to an Array

    Hi,

    I want to grab data from a .csv file and add all the data to the array. I have the part where I'm grabbing the data from the .csv file but the array issue I haven't totally figured out.

    Currently, I'm not adding all the data just the last row of data to the array and need help trying to add all the data to the array. Can someone please help me accomplish this. This is what I have:

    [PHP]
    global $info;
    ini_set("auto_d etect_line_endi ngs", 1);
    $current_row = 1;
    $handle = fopen("adduser. csv", "r");
    $info="";
    while ( ($data = fgetcsv($handle , 10000, ",") ) !== FALSE )
    {
    $number_of_fiel ds = count($data);
    if ($current_row == 1)
    {
    //Header line
    for ($c=0; $c < $number_of_fiel ds; $c++)
    {
    $header_array[$c] = $data[$c];
    }
    }
    else
    {
    //Data line
    for ($c=0; $c < $number_of_fiel ds; $c++)
    {
    $data_array[$header_array[$c]] = $data[$c];
    }
    print_r($data_a rray);
    $info = $data_array;
    }
    $current_row++;
    }
    //buildBatchXML($ data_array);

    echo "<br><br>Th is is new data:<br><br>";
    foreach($info as $key => $value)
    {
    echo "$key - $value<br>";
    }

    fclose($handle) ;

    ?>

    [/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    The array only contains the date of the last csv row because you use a 'flat' array, i.e. its format is:
    Code:
    $data_array Array {
           ['heading_1'] = col_1_data
           ['heading_2'] = col_2_data
           ['heading_x'] = col_x_data
    }
    while what you want is:
    Code:
    $data_array Array {
           [0]  ['heading_1'] = row1_col_1_data
                ['heading_2'] = row1_col_2_data
                ['heading_x'] = row1_col_x_data
           [1]  ['heading_1'] = row2_col_1_data
                ['heading_2'] = row2_col_2_data
                ['heading_x'] = row2_col_x_data
           [n]  ['heading_1'] = rown_col_1_data
                ['heading_2'] = rown_col_2_data
                ['heading_x'] = rown_col_x_data
    }
    so use the $current_row counter as the first level index for the output array. You'll have to initizalize it to 0 instead of 1.
    The array walking should have an extra 'foreach' because you also have to walk the numeric indexes. I removed the $info because it serves no purpose, only costs storage. See next code .
    [php]
    <?php
    global $info;
    ini_set("auto_d etect_line_endi ngs", 1);
    // ======= Next statement changed =========//
    $current_row = 0;
    $handle = fopen("adduser. csv", "r");
    while ( ($data = fgetcsv($handle , 10000, ",") ) !== FALSE )
    {
    $number_of_fiel ds = count($data);
    // ======= Next statement changed =========//
    if ($current_row == 0)
    {
    //Header line
    for ($c=0; $c < $number_of_fiel ds; $c++)
    {
    $header_array[$c] = $data[$c];
    }
    }
    else
    {
    //Data line
    for ($c=0; $c < $number_of_fiel ds; $c++)
    {
    // ======= Next statement changed =========//
    $data_array[$current_row][$header_array[$c]] = $data[$c];
    }
    print_r($data_a rray);
    }
    $current_row++;
    }
    //buildBatchXML($ data_array);

    echo "<br><br>Th is is new data:<br><br>";
    // ======= Next 2 statements added/changed =========//
    foreach ($data_array as $csvrow) {
    foreach($csvrow as $key => $value)
    {
    echo "$key - $value<br>";
    }
    }
    fclose($handle) ;

    ?>
    [/php]
    Ronald

    Comment

    • lilbit02
      New Member
      • Nov 2007
      • 22

      #3
      thank you so much that really helped my understanding.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Originally posted by lilbit02
        thank you so much that really helped my understanding.
        Glad to be of help. Are you set now? See you again.

        Ronald

        Comment

        Working...