ghostrider 0 Report post Posted April 14, 2007 This is the fourth part of my PHP tutorial. You can view the last three here:Third Tutorial (Form Data and If statements) Second Tutorial (Variables)First Tutorial (What is PHP?)Intro To PHPTutorial 4 - ArraysReleased 4/13/07By Chris Feilbach aka GhostRiderContact Info:E-mail: assembler7@gmail.comAIM: emptybinder78Yahoo: drunkonmarshmellowsWebsite: http://forums.xisto.com/no_longer_exists/Hello once again! During this tutorial I'm going to teach you about arrays. This tutorial does not have a project with it.Remember back to my second tutorial, when you learned about variables. Variables allow programmers to store data, which then allows to do stuff with it. However variables can only store one value at a time. Arrays however, allow you to store more than one value at a time in the same variable, which makes arrays execeptionally useful in any langauge.PHP has exactly 75 functions and statements that deal with arrays. They either sort them in different ways, tell you how many elements (values) are in them, or do a bunch of other stuff. We'll use maybe 3 or 4 of them in this tutorial.Remember back to the last lesson when we learned about form data. The $_GET variable and the $_POST variable are arrays. They are called associative arrays because they have a key that is NOT a number. Arrays work very similar to form data in the sense that they two are in the (name=value) pair, meaning that each value has a name that allows you to access.An element is a value stored in an array. Lets say we have the following web address:http://forums.xisto.com/no_longer_exists/Lets get the GET data from this $mode = $_GET['mode']; // $mode = tutorial$submode = $_GET['submode']; // $submode = 1 Our keyes are mode and submode. In associative arrays they need to be put around single quotes. If you don't put single quotes around it you get an annoying message from PHP saying that you need to.The second type of array is called a numerical array. In this case the keyes are integers, starting on whatever integer you want. Any floating number (a number with a decimal after it) will be truncated if you try to use it. Do yourself a favor and always use whole numbers. Unlike associative arrays, the key does not need to be put inside of single quotes.While a numberical array can start on any number, I advise you to start on 0. Programmers start counting from 0, not 1. From now on you will always see me start my numerical arrays with 0. I highly suggest you do the same.Arrays are created in one of two ways. You can simply define them like so:$ourfirstarray['temp'] = 38593;$ourfirstarray['name'] = "Chris";$ourfirstarray['temp1'] = 88.55555;$oursecondarray[0] = 27;$oursecondarray[1] = "Hello";$oursecondarray[2] = $ourfirstarray; // <-- Yes, you can do that in PHP. Take a look at the last line of code. This now creates something special, something called a multidimensional array. These are very useful, but we won't be using that often yet. They are generally used when code becomes more complex than ours, and has more data to process. Multidimensional arrays can have as many dimensions as you want, but they take up a lot of memory. Use them sparingly and only if you feel you need to.We can access the data in $ourfirstarray throught $oursecondarray$temp = $oursecondarray[2]['temp']; // $temp = 38593$temp = $oursecondarray[2]['name']; // $temp = Chris// Be careful however$temp = $ourfirstarray[2]['temp']; // $temp = NULL (nothing) Just because one array is multidimensional does not mean the other array is.There is another way of making an array. You can use the arrange function.Lets create an array with the keys first and last with the values "Enter your first name." and "Enter your last name."$array = array("first" => "Enter your first name.", "last" => "Enter your last name.");print $array['first']; // Prints Enter your first name. You can define as many key/value pairs as your wish using the array function. What about multidimensional arrays? You can create those, too.$array = array("secondarray" => array("val1" => 22, "val2" => "Data"));print $array['secondarray']['val1']; // Prints 22 You can also create numerical arrays using the array function. Do not put them in quotes. The array will then be considered associate if you do. Also, if you do not want to specify a new number everytime you define a key, you can specify a number, and then simply continue with printing the values, and not the keys. For example:$array = array(0 => 1, 2, 3, 4);print $array[0]; // Prints 1print $array[1]; // Prints 2print $array[2]; // Prints 3 This concludes this tutorial. Next tutorial we'll cover loops, and then the tutorial after that cookies. And after that, we'll start our first major project. Review a lot. You are at the point where you should know enough now to start to develop your own small scripts. Play around with it. If you develop something really cool, send me a link or code. Share this post Link to post Share on other sites
Laurie 0 Report post Posted April 14, 2007 I appreciate you sharing this course of tutorials. The thing I need that you dont provide is the idea of WHAT to program. Any one with suggestions as to possible concepts, please share them with me. I would like to practice using my programming skills. Share this post Link to post Share on other sites