0

I am trying to split an array if one of the lines has an empty value. My array is being exported to a csv and has multiple lines of data, if the data is not complete it will be sent to a uncomplete csv and if it is complete it will be sent a complete csv.

This is what my array structure looks like the blank field on the first line is on email (this line should be split out of the array:

array(2) { 
    [0]=> array(6) { 
        ["Username"]=> string(47) " STARRY NIGHT CINEMA - RISE OF THE GUARDIANS " 
        ["Email"]=> string(0) "" 
        ["Location"]=> string(1) "1" 
        ["Type"]=> int(1) 
        ["Title"]=> string(47) " STARRY NIGHT CINEMA - RISE OF THE GUARDIANS " 
        ["Description"]=> string(491) "the Tooth Fairy"
    }
    [1]=> array(6) { 
        ["Username"]=> string(26) "Maui Nui Botanical Gardens" 
        ["Email"]=> string(18) "info@mnbg.org" 
        ["Location"]=> string(1) "1" 
        ["Type"]=> int(1) 
        ["Title"]=> string(26) "Maui Nui Botanical Gardens" 
        ["Description"]=> string(50) "Conserving Hawaiian Plants & Cultural Heritage"
    }
} 

Writing to my csv:

    $fp = fopen('entries.csv', 'w');
    foreach ($entries as $fields) {
        fputcsv($fp, $fields);
    }

    fclose($fp);

    if (!copy("http://www.simonstaton.co.uk/entries.csv", "entries.csv")) {
        echo ("failed to copy file");
    };

I have no idea where to start with this and as to what function I should use so any advice is greatly appreciated.

Simon

4

3 に答える 3

0

You can check if any of the values in your array is empty like this:

in_array('', $array)

so it then you can do a simple if

if(in_array('', $array)) {
    //Do whatever you have to do to slip your array. I don't understand exactly what you want the end result to look like .
}
于 2013-03-24T23:19:26.070 に答える
0

First pack your CSV data into an array using fgetcsv(). Then use a loop to search for "". Exemplar:

$row = 1;
if (($resource = fopen("example.csv", "r")) !== false) {
    while (($data = fgetcsv($resource, 1000, ",")) !== false) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            if ($data[$c] == "") { 
               // operate 
            }
        }
    }
    fclose($handle);
}

By the way, don't use the split() function. Use preg_split(). http://php.net/manual/en/function.fputcsv.php

于 2013-03-24T23:21:32.653 に答える
0

To separate the entries by complete or incomplete you can open two files for writing. One to contain each.

// filenames
$fn_complete = 'entries.csv';
$fn_incomplete = 'incomplete_entries.csv';

// file pointers
$fp_complete = fopen($fn_complete, 'w');
$fp_incomplete = fopen($fn_incomplete, 'w');

// write CSVs
foreach ($entries as $fields) {
    if (in_array('', $fields))
        fputcsv($fp_incomplete, $fields);
    else
        fputcsv($fp_complete, $fields);
}

fclose($fp_complete);
fclose($fp_incomplete);

if (!copy('http://www.simonstaton.co.uk/' . $fn_complete, $fn_complete)) {
    echo ('failed to copy file ' . $fn_complete);
};
if (!copy('http://www.simonstaton.co.uk/' . $fn_incomplete, $fn_incomplete)) {
    echo ('failed to copy file ' . $fn_incomplete);
};
于 2013-03-24T23:28:31.290 に答える