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 trial
Peter Mumford
7,774 PointsSimple array question
This code is in the Build A Simple PHP Application series:
<?php
$flavors = array("Cake Batter","Cookie Dough");
foreach ($flavors as $a => $b) {
echo $a;
}
?>
I'm trying to understand what it does, and why. It seems to me that it creates the $a variable. But it wants to assign a series of $b values to $a. But $b is undefined. What's going on?
Randy Hoyt
Treehouse Guest TeacherGood question! I'm not sure there's an easy way to keep this straight. You can always reference a specific element in an array like this:
$products[$product_id]["sku"]
That element gets treated just like any other variable. You can echo it out or you can assign a value to it:
$products[$product_id]["sku"] = $product_id;
echo $products[$product_id]["sku"];
The arrow is used for more array-specific actions, like creating a new array or looping through an array. You couldn't do these things with just regular variables. In array-specific commands, the arrow works to express the key-value relationship:
$product = array( "sku" => 101, "name" => "Red Frog Shirt");
foreach ($product as $key => $value) {
echo $key . ": " . $value . "\n";
}
Does that help?
Peter Mumford
7,774 PointsThanks, Randy! That (sort of) explains it.
What course are you doing next? I'd love it if you did some vids showing how to replace the products array with a mysql database, and have an admin user add products. Is that a possibility? Mike the Frog is probably going to want to manage his own content I think.
Randy Hoyt
Treehouse Guest TeacherI'm currently writing a PHP/MySQL course. It won't cover how to build the admin interface -- at least not at first -- but it will show how to store the data in a database and load that into the website.
Peter Mumford
7,774 PointsWow. The man delivers the goods: http://teamtreehouse.com/library/programming/using-php-with-mysql
Randy Hoyt
Treehouse Guest TeacherHa! I look forward to hearing your feedback on the new course, Peter Mumford.
Peter Mumford
7,774 PointsI finished the mysql course. Good stuff! The last section, where you re-factored all the functions was very helpful. I think all the repetitive coding helped me get the mechanics of querying the database down better.
I'm looking forward to the admin area course.
1 Answer
J A
6,092 Pointsthe line ($flavours as $a => $b) is using the format $key => $value. Your array doesn't set keys, so it would be the default [0], [1], [2] ect. So for your array it would be [0] => "Cake Batter", [1] => "Cookie Dough".
Peter Mumford
7,774 PointsOk.. $key => $value explains it. But I was under the impression that the => operator assigns a value to a key. In this use it does not do that. [edit] Maybe its more accurate to say => defines the relationship between the two variables?
Randy Hoyt
Treehouse Guest TeacherYes, that sounds accurate. The => represents the relationship between a key and a value, which may be used with different results in an array() command or in a foreach loop.
Peter Mumford
7,774 PointsPeter Mumford
7,774 PointsNow I'm working through the Enhancing a Simple PHP Application. And I come to more about tossing arrays around, and I get to the part where the SKU is copied from the top-level key to inserted into the value of the key sku at the second level:
$products[$product_id]["sku"] = $product_id;What I don't understand is why, in this instance, the key and value are joined by a = instead of a =>. The equals sign works, and the arrow breaks it, but why?