0

I'm trying to understand exactly why fgetcsv is failing here. The outputted array appears to be broken.

$csv_input_file: Sacramentorealestatetransactions.csv

Example of a csv record line in this file:

7933 DAFFODIL WAY,CITRUS HEIGHTS,95610,CA,3,2,1216,Residential,Thu May 15 00:00:00 EDT 2008,235000,38.708824,-121.256803

fgetcsv:

$file = fopen($csv_input_file,"r");


if($method == 'fgetcsv'){


    $$output_array = array();

    while(! feof($file))
    {
        $$output_array = fgetcsv($file);
    }
}

example (excerpt) of elements from print_r output of fgetcsv:

...
    10085 ATKINS DR-121.246743
        [10616] => ELK GROVE
        [10617] => 95757
        [10618] => CA
        [10619] => 3
        [10620] => 2
        [10621] => 1302
        [10622] => Residential
        [10623] => Thu May 15 00:00:00 EDT 2008
        [10624] => 219794
        [10625] => 38.390893
    9185 CERROLINDA CIR.437821
        [10627] => ELK GROVE
        [10628] => 95758
        [10629] => CA
        [10630] => 3
        [10631] => 2
        [10632] => 1418
        [10633] => Residential
        [10634] => Thu May 15 00:00:00 EDT 2008
        [10635] => 220000
        [10636] => 38.424497
    9197 CORTINA CIR121.426595
        [10638] => ROSEVILLE
        [10639] => 95678
        [10640] => CA
        [10641] => 3
        [10642] => 2
        [10643] => 0
        [10644] => Condo
        [10645] => Thu May 15 00:00:00 EDT 2008
        [10646] => 220000
        [10647] => 38.793152
    5429 HESPER WAY-121.290025
        [10649] => CARMICHAEL
        [10650] => 95608
        [10651] => CA
        [10652] => 4
        [10653] => 2
        [10654] => 1319
        [10655] => Residential
        [10656] => Thu May 15 00:00:00 EDT 2008
        [10657] => 220000
        [10658] => 38.665104
    1178 WARMWOOD CT121.315901
        [10660] => GALT
        [10661] => 95632
        [10662] => CA
        [10663] => 4
        [10664] => 2
        [10665] => 1770
        [10666] => Residential
        [10667] => Thu May 15 00:00:00 EDT 2008
        [10668] => 220000
        [10669] => 38.289544
    4900 ELUDE CT> -121.284607
        [10671] => SACRAMENTO
        [10672] => 95842
        [10673] => CA
        [10674] => 4
        [10675] => 2
        [10676] => 1627
        [10677] => Residential
        [10678] => Thu May 15 00:00:00 EDT 2008
        [10679] => 223000
        [10680] => 38.69674
    3557 SODA WAY> -121.350519
        [10682] => SACRAMENTO
        [10683] => 95834
        [10684] => CA
        [10685] => 0
        [10686] => 0
        [10687] => 0
        [10688] => Residential
        [10689] => Thu May 15 00:00:00 EDT 2008
        [10690] => 224000
        [10691] => 38.631026
    3528 SAINT GEORGE DR501879
        [10693] => SACRAMENTO
        [10694] => 95821
        [10695] => CA
        [10696] => 3
        [10697] => 1
        [10698] => 1040
        [10699] => Residential
        [10700] => Thu May 15 00:00:00 EDT 2008
        [10701] => 224000
        [10702] => 38.629468
    7381 WASHBURN WAY21.376445
        [10704] => NORTH HIGHLANDS
        [10705] => 95660
        [10706] => CA
        [10707] => 3
        [10708] => 1
        [10709] => 960
        [10710] => Residential
        [10711] => Thu May 15 00:00:00 EDT 2008
        [10712] => 224252
        [10713] => 38.70355
    2181 WINTERHAVEN CIR375103
        [10715] => CAMERON PARK
        [10716] => 95682
        [10717] => CA
        [10718] => 3
        [10719] => 2
        [10720] => 0
        [10721] => Residential
        [10722] => Thu May 15 00:00:00 EDT 2008
        [10723] => 224500
        [10724] => 38.69757
    7540 HICKORY AVE120.995739
        [10726] => ORANGEVALE
        [10727] => 95662
        [10728] => CA
        [10729] => 3
        [10730] => 1
        [10731] => 1456
        [10732] => Residential
        [10733] => Thu May 15 00:00:00 EDT 2008
        [10734] => 225000
        [10735] => 38.703056
    5024 CHAMBERLIN CIR.235221
        [10737] => ELK GROVE
        [10738] => 95757
        [10739] => CA
        [10740] => 3
        [10741] => 2
        [10742] => 1450
        [10743] => Residential
        [10744] => Thu May 15 00:00:00 EDT 2008
        [10745] => 228000
        [10746] => 38.389756
    2400 INVERNESS DR21.446246
        [10748] => LINCOLN
        [10749] => 95648
        [10750] => CA
        [10751] => 3
        [10752] => 2
        [10753] => 1358
        [10754] => Residential
        [10755] => Thu May 15 00:00:00 EDT 2008
        [10756] => 229027
        [10757] => 38.897814
    5 BISHOPGATE CT-121.324691
        [10759] => SACRAMENTO
        [10760] => 95823
        [10761] => CA
        [10762] => 4
        [10763] => 2
        [10764] => 1329
        [10765] => Residential
        [10766] => Thu May 15 00:00:00 EDT 2008
        [10767] => 229500
        [10768] => 38.467936
    5601 REXLEIGH DR121.445477
        [10770] => SACRAMENTO
        [10771] => 95823
        [10772] => CA
        [10773] => 4
        [10774] => 2
        [10775] => 1715
        [10776] => Residential
        [10777] => Thu May 15 00:00:00 EDT 2008
        [10778] => 230000
        [10779] => 38.445342
    1909 YARNELL WAY121.441504
        [10781] => ELK GROVE
        [10782] => 95758
        [10783] => CA
        [10784] => 3
        [10785] => 2
        [10786] => 1262
        [10787] => Residential
        [10788] => Thu May 15 00:00:00 EDT 2008
        [10789] => 230000
        [10790] => 38.417382
    9169 GARLINGTON CT1.484325
        [10792] => SACRAMENTO
        [10793] => 95829
        [10794] => CA
        [10795] => 4
        [10796] => 3
        [10797] => 2280
        [10798] => Residential
        [10799] => Thu May 15 00:00:00 EDT 2008
        [10800] => 232425
        [10801] => 38.457679
    6932 RUSKUT WAY-121.35962
        [10803] => SACRAMENTO
        [10804] => 95823
        [10805] => CA
        [10806] => 3
        [10807] => 2
        [10808] => 1477
        [10809] => Residential
        [10810] => Thu May 15 00:00:00 EDT 2008
        [10811] => 234000
        [10812] => 38.499893
    7933 DAFFODIL WAY21.45889
        [10814] => CITRUS HEIGHTS
        [10815] => 95610
        [10816] => CA
        [10817] => 3
        [10818] => 2
        [10819] => 1216
        [10820] => Residential
        [10821] => Thu May 15 00:00:00 EDT 2008
        [10822] => 235000
        [10823] => 38.708824
    8304 RED FOX WAY121.256803
        [10825] => ELK GROVE
        [10826] => 95758
        [10827] => CA
        [10828] => 4
        [10829] => 2
        [10830] => 1685
        [10831] => Residential
        [10832] => Thu May 15 00:00:00 EDT 2008
        [10833] => 235301
        [10834] => 38.417
    3882 YELLOWSTONE LN.397424
        [10836] => EL DORADO HILLS
        [10837] => 95762
        [10838] => CA
        [10839] => 3
        [10840] => 2
        [10841] => 1362
        [10842] => Residential
        [10843] => Thu May 15 00:00:00 EDT 2008
        [10844] => 235738
        [10845] => 38.655245
        [10846] => -121.075915
    )

I'm not even sure what this is. The street address is merged with a longitude value from another line. Why should I logically expect this result, can someone please walk me through the logic of what is happening to give such unexpected output?

Edit: the fully expanded code from which the excerpt above derives:

include('/home/xxxxxx/public_html/wp-content/uploads/wpallimport/files/rubley_csv_parse.php'); 

// GET THE ARRAY FROM CSV FILE
function csvfile2array($csv_input_file,$output_array,$method,$preview){
    //$method = 'rubley', 'fetgcsv'
    //$preview = y, n

    $file = fopen($csv_input_file,"r");


    if($method == 'fgetcsv'){


        $$output_array = array();

        while(! feof($file))
        {
            $$output_array = fgetcsv($file);
        }
    }


    if($method == 'rubley'){

    $str = file_get_contents($csv_input_file);

    $$output_array = parse_csv($str);

        }


    if ($preview == 'y'){
        print_r($$output_array);
        }

    fclose($file);

}

Call function:

csvfile2array('/home/xxxxxx/public_html/wp-content/uploads/wpallimport/files/yyyyyy/Sacramentorealestatetransactions.csv','realest_rubleyed_array','fgetcsv','y');

To those suggesting this is a newline-related problem, this may be useful - it seems newlines are written as CR which is strange, I've only ever seen lines terminated as CR LF or just LF, never as CR before:

Screenshot of Notepad++ view revealing terminating linebreak CR

shau
  • 15
  • 4
  • @mario I've just added the full code from which the excerpt was taken. See original post edit. The variable variable makes use of an argument provided by the user. I'm atttempting to convert an entire csv to array. – shau Jan 19 '20 at 08:30
  • 1
    There are multiple things that look wrong in your code. One among others is you're not returning anything (you're "pointing" toward a variable name that can't be written into like that). Why not just return the result? As for your issue as hand, you're overwriting `$$output_array` with every new line, which can't be right. – Jeto Jan 19 '20 at 08:36
  • @Jeto I thought `while(! feof($file)){ $$output_array = fgetcsv($file); }` would append rather than overwrite to the array? – shau Jan 19 '20 at 08:43
  • I would suggest returning the array rather than the method you are using. So creating a temporary array in the function and building the data up in that and just `return $output_array;` at the end. Use `$output_array[] = fgetcsv($file);` to build it in the CSV part. – Nigel Ren Jan 19 '20 at 08:46
  • @mario How did you deduce my problem relates to newlines? I'm not following the logic. Anyone? – shau Jan 19 '20 at 09:45
  • From the shared sample really. Albeit the consecutive indexes and conjoined lines could have been a giveaway. -- Anyway, throw that function away. It's a oneliner really: `$array = array_map("str_getcsv", preg_split("/\R/", file_get_contents($fn)));` – mario Jan 19 '20 at 09:54
  • @mario. Your solution works. But I have an extension question. The basic pattern of the malformed array is that the longitude value is tacked onto the end of the street address (a reordering of the two adjoined fields). But in addition, sometimes you lose the negative sign of the longitude, other times you lose everything before the decimal point, and other times, the whole longitude value is preserved. What's the logical principle governing this apparent variability in the malformed pattern that we get? – shau Jan 19 '20 at 15:10

0 Answers0