Newbie here - please explain this code :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanmc
Forum Newbie
Posts: 1
Joined: Wed Sep 03, 2008 8:34 pm

Newbie here - please explain this code :)

Post by ryanmc »

ok, so i inherited a program and i would appreciate if someone could explain the bold code to me.

Code: Select all

    $handle = fopen("foo.csv", "r");
    while (($cols = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
 
        #state|city
        [b]$CITY["$cols[3]"]["$cols[2]"] = 1;[/b]    what does the = 1 mean?
        #city|store
        [b]$STORE["$cols[7]"]["$cols[2] - $cols[6]"] = 1;[/b]   does the dash (-) mean columns 2 thru 6?
        $DATE["$cols[2] - $cols[6]"]["$cols[17] $cols[18] - $cols[19]"] = 1;[/b]
    }
 
sample input:

Store #,Address,City,ST,Zip,Immunizer,Provider,Clinic Date,Start Time,End Time
801,311 W. College Dr. ,Durango,CO,81301,Susan H,RPH.,"Thursday, October 09, 2008",3:00 PM,7:00 PM
801,311 W. College Dr. ,Durango,CO,81301,Susan H,RPH.,"Saturday, October 25, 2008",10:00 AM,2:00 PM
801,311 W. College Dr. ,Durango,CO,81301,Susan H,RPH.,"Saturday, November 08, 2008",10:00 AM,2:00 PM
803,7055 Austin Bluffs Pkway,COLORADO SPRINGS ,CO,80923,,Mollen Immunization Clinics,"Wednesday, October 01, 2008",11:00 AM,3:00 PM
803,7055 Austin Bluffs Pkway,COLORADO SPRINGS ,CO,80923,,Mollen Immunization Clinics,"Thursday, October 02, 2008",11:00 AM,3:00 PM
803,7055 Austin Bluffs Parkway,Colorado Springs,CO,80923,Rob G ,RPH.,"Monday, October 13, 2008",10:00 AM,2:00 PM
804,1451 W. Eisenhower Blvd.,LOVELAND ,CO,80538,,Mollen Immunization Clinics,"Monday, October 06, 2008",11:00 AM,3:00 PM
804,1451 W. Eisenhower Blvd.,LOVELAND ,CO,80538,,Mollen Immunization Clinics,"Tuesday, October 07, 2008",11:00 AM,3:00 PM
804,1451 W. Eisenhower Blvd.,LOVELAND ,CO,80538,,Mollen Immunization Clinics,"Monday, October 13, 2008",11:00 AM,3:00 PM
804,1451 W. Eisenhower Blvd.,LOVELAND ,CO,80538,,Mollen Immunization Clinics,"Tuesday, October 14, 2008",11:00 AM,3:00 PM
804,1451 Eisenhower Blvd ,Loveland,CO,80537,Dawn H,RPH.,"Tuesday, October 21, 2008",10:00 AM,2:00 PM
804,1451 Eisenhower Blvd ,Loveland,CO,80537,Dawn H,RPH.,"Wednesday, November 05, 2008",3:00 PM,7:00 PM
808,555 W South Boulder Rd.,LAFAYETTE ,CO,80026,,Mollen Immunization Clinics,"Wednesday, October 08, 2008",11:00 AM,3:00 PM
808,555 W South Boulder Rd.,LAFAYETTE ,CO,80026,,Mollen Immunization Clinics,"Thursday, October 09, 2008",11:00 AM,3:00 PM
808,555 W South Boulder Rd.,LAFAYETTE ,CO,80026,,Mollen Immunization Clinics,"Wednesday, October 15, 2008",11:00 AM,3:00 PM
808,555 W South Boulder Rd.,LAFAYETTE ,CO,80026,,Mollen Immunization Clinics,"Thursday, October 16, 2008",11:00 AM,3:00 PM

this is the output i'm getting:

Array ( [ST] => Array ( [City] => 1 ) [CO] => Array ( [Durango] => 1 [COLORADO SPRINGS ] => 1 [Colorado Springs] => 1 [LOVELAND ] => 1 [Loveland] => 1 [LAFAYETTE ] => 1 [Lafayette] => 1 [Louisville] => 1 [FORT COLLINS ] => 1 [Fort Collins] => 1 [Evergreen] => 1 [Pueblo] => 1 [DENVER ] => 1 [Denver] => 1 [LITTLETON ] => 1 [Littleton] => 1 [AURORA ] => 1 [Aurora] => 1 [Northglenn] => 1 [Greenwood Village] => 1 [LONGMONT ] => 1 [THORNTON ] => 1 [Thornton] => 1 [Arvada] => 1 ) [NM] => Array ( [GALLUP] => 1 [RIO RANCHO] => 1 [Rio Rancho] => 1 [ALBUQUERQUE] => 1 [Albuquerque] => 1 [CARLSBAD] => 1 [LAS CRUCES] => 1 [Las Cruces] => 1 [SILVER CITY] => 1 [CLOVIS] => 1 [ Albuquerque] => 1 [Los Lunas] => 1 [LOS LUNAS] => 1 [TAOS] => 1 [SANTA FE] => 1 [Santa Fe] => 1 [HOBBS] => 1 [ROSWELL] => 1 [Roswell] => 1 [FARMINGTON] => 1 ) [AZ] => Array ( [LITCHFIELD PARK] => 1 [GILBERT] => 1 [YUMA] => 1 [TEMPE] => 1 [GLENDALE] => 1 [MESA] => 1 [PHOENIX] => 1 [PRESCOTT] => 1 [SCOTTSDALE] => 1 [TUCSON] => 1 [PRESCOTT VALLEY] => 1 [FLAGSTAFF] => 1 [CASA GRANDE] => 1 [PEORIA] => 1 [SURPRISE] => 1 [CHANDLER] => 1 [LAKE HAVASU CITY] => 1 ) [TX] => Array ( [El Paso] => 1 [COLLEGE STATION] => 1 [KERRVILLE] => 1 [NEW BRAUNFELS] => 1 [Murphy] => 1 [MURPHY] => 1 [SAGINAW] => 1 [Saginaw] => 1 [DALLAS] => 1 [Dallas] => 1 [Euless] => 1 [EULESS] => 1 [CARROLLTON] => 1 [Carrollton] => 1 [LONGVIEW] => 1 [Longview] => 1 [DENISON] => 1 [GARLAND] => 1 [PLANO] => 1 [Plano] => 1 [FORT WORTH] => 1 [RICHARDSON] => 1 [Sherman] => 1 [SHERMAN] => 1 [MESQUITE] => 1 [Mesquite] => 1 [CEDAR HILL] => 1 [TEXARKANA] => 1 [Arlington] => 1 [ARLINGTON] => 1 [HURST] => 1 [Hurst] => 1 [ROANOKE] => 1 [LAKE WORTH] => 1 [DENTON] => 1 [Denton] => 1 [Midland] => 1 [MIDLAND] => 1 [ODESSA] => 1 [SAN ANGELO] => 1 [ABILENE] => 1 [Abilene] => 1 [DESOTO] => 1 [DeSoto] => 1 [Ft. Worth] => 1 [WEATHERFORD] => 1 [ALLEN] => 1 [Allen] => 1 [BENBROOK] => 1 [Benbrook] => 1 [CORINTH] => 1 [Corinth] => 1 [WATAUGA] => 1 [ROWLETT] => 1 [Rowlett] => 1 [Fort Worth] => 1 [AMARILLO] => 1 [Amarillo] => 1 [Tyler] => 1 [San Angelo] => 1 [FRISCO] => 1 [Frisco] => 1 [GRAND PRAIRIE] => 1 [Grand Prairie] => 1 [MC KINNEY] => 1 [McKinney] => 1 [WICHITA FALLS] => 1 [WYLIE] => 1 [Wylie] => 1 [CROWLEY] => 1 [Crowley] => 1 [BEDFORD] => 1 [Bedford] => 1 [COLLEYVILLE] => 1 [Colleyville] => 1 [BURLESON] => 1 [Burleson] => 1 [Lewisville] => 1 [LEWISVILLE] => 1 [Southlake] => 1 [AZLE] => 1 [CLEBURNE] => 1 ) [LA] => Array ( [BATON ROUGE] => 1 [HAMMOND] => 1 [ALEXANDRIA] => 1 [LAKE CHARLES] => 1 [LAFAYETTE] => 1 [BROUSSARD] => 1 [SHREVEPORT] => 1 [BOSSIER CITY] => 1 ) [AR] => Array ( [TEXARKANA] => 1 ) [FL] => Array ( [CLEARWATER] => 1 [ALTAMONTE SPRINGS] => 1 [GAINESVILLE] => 1 [Gainesville] => 1 [OAKLAND PARK] => 1 [ORLANDO] => 1 [TAMPA] => 1 [LAKE WORTH] => 1 [WEST PALM BEACH] => 1 [West Palm Beach] => 1 [VENICE] => 1 [Venice] => 1 [LONGWOOD] => 1 [BRADENTON] => 1 [VERO BEACH] => 1 [APOPKA] => 1 [Apopka] => 1 [PEMBROKE PINES] => 1 [LAKE MARY] => 1 [BOCA RATON] => 1 [MOUNT DORA] => 1 [Mt. Dora] => 1 [WINTER SPRINGS] => 1 [DELRAY BEACH] => 1 [OCALA] => 1 [EAST LARGO] => 1 [KISSIMMEE] => 1 [OVIEDO] => 1 [WINTER PARK] => 1 [MELBOURNE] => 1 [VALRICO] => 1 [Orlando] => 1 [SARASOTA] => 1 [Sarasota] => 1 [PORT ST. LUCIE] => 1 [SANFORD] => 1 [Sanford] => 1 [LOXAHATCHEE] => 1 [ESTERO] => 1 [RIVERVIEW] => 1 [Riverview] => 1 [BONITA SPRINGS] => 1 [KEY WEST] => 1 [Lake Worth] => 1 [Tallahassee] => 1 [Apopaka] => 1 ) ) CASA GRANDE CHANDLER FLAGSTAFF GILBERT GLENDALE LAKE HAVASU CITY LITCHFIELD PARK MESA PEORIA PHOENIX PRESCOTT PRESCOTT VALLEY SCOTTSDALE SURPRISE TEMPE TUCSON YUMA

Thanks! :D :D :D :D
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Newbie here - please explain this code :)

Post by andyhoneycutt »

Code: Select all

 
        [b]$CITY["$cols[3]"]["$cols[2]"] = 1;[/b]    //what does the = 1 mean?
        // The " = 1" is an assignment. $variable[even][multidimensional][arrays] = SOMEVALUE; is always an assignment.
        // So, what this is doing, and I'm not sure why the double-quotes, is setting the two-dimensional array key at
        // "$cols[3]" (consider this the x-plot), "$cols[2]" (consider this the y-plot) (as if on a grid) to the value of 1.
        #city|store
        [b]$STORE["$cols[7]"]["$cols[2] - $cols[6]"] = 1;[/b]   //does the dash (-) mean columns 2 thru 6?
        // I may be wrong, but I believe that's explicit to languages like Perl, and Perl's derivatives, such as Python.
        // I would assume subtraction is going on here, but having never found the need to tell PHP to interpret
        // a value as a key in an array in [b]this fashion[/b] I can't say I'm exactly sure what's going on. If I assume
        // what I believe to be going on: they are saying the key in $STORE at $cols[7] (x on a grid), (the value of $cols[2] MINUS $cols[6]) (y  on a grid) is set to 1.
    }
 
All you're looking at here is a multi-dimensional array no matter how the programmer decides to reference the indexes. You can look at it however it makes sense to you, but basically a multi-dimensional array is a grouping of arrays within arrays. $STORE[0] is an array that, say, contains 2 other arrays.

I hope this helps, it's late!
Andy.
Post Reply