Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDario Preglej
9,771 Pointsfgetcsv function
Hello guys!
In this course Alena used : $fh = fopen('data/csv/people.csv', 'r'); $header = fgetcsv($fh); extract(array_flip($header));
and then: while(($contact = fgetcsv($fh)) !== false){}
I don't understand how is that when she used $contact[$first] she got the string "Alena".
var_dump($contact); results: array(5) { [0]=> string(5) "Alena" [1]=> string(8) "Holligan" [2]=> string(14) "sketchings.com" [3]=> string(10) "sketchings" [4]=> string(25) "/img/jV-j2pxp_400x400.jpg" }
var_dump($first); results: int(0)
var_dump($contact[$first]); results: string(5) "Alena"
HOW??
2 Answers
pems
Full Stack JavaScript Techdegree Student 15,337 PointsHey, how's it going.
She used two functions.
1. array_flip() to flip the properties/keys with the values in the array...
ex.
if
$temp1 = array(2) { [0]=>"Hey", [1]=>"there" };
then
array_flip($temp1) = array(2) { ["Hey"]=>0, ["there"]=>1 };
2. extract() to convert the keys/properties of an array into usable variables
**the VALUES of these variables are the values from the array**...
ex.
if
$temp2 = array(2) { ["Flavor"]=>"Vanilla",
["Topping"]=>"Sprinkles" };
then
extract($temp2) would return two usable variables(you don't see these get
returned, that's why it might have been confusing),
variable1 is $Flavor
variable2 is $Topping
if you echo these variables you would get
echo $Flavor;
echo $Topping;
-----OUTPUT------->
Vanilla
Sprinkles
So she used these two functions to create five variables...
flip_array() extract()
[0]=> first ---------> ["first"]=> 0 ----------> $first = 0
[1]=> last ---------> ["last"]=> 1 ----------> $last = 1
[2]=> website ---------> ["website"]=> 2 ----------> $website = 2
[3]=> twitter ---------> ["twitter"]=> 3 ----------> $twitter = 3
[4]=> img ---------> ["img"]=> 4 ----------> $img = 4
...and when using these variables as keys for the $contact array, we get...
$contact[$first] = "Alena"
$contact[$last] = "Holligan"
$contact[$website] = "sketchings.com"
$contact[$twitter] = "http://twitter.com/sketchings"
$contact[$img] = "/img/<img_name>"
She could have just skipped all of these steps and used 0,1,2,3,4 as the keys for the $contact array
$contact[0] = "Alena"
$contact[1] = "Holligan"
$contact[2] = "sketchings.com"
$contact[3] = "http://twitter.com/sketchings"
$contact[4] = "/img/<img_name>"
, but the functions she used here makes the code more readable (it's easier to see what's going on)
Dario Preglej
9,771 PointsYes i undestand that but why echo-ing $contract["first"] she dont get 0? Why she get "Alena"?