draco2317 Posted August 9, 2006 Share Posted August 9, 2006 I have read about arrays, and see how they work, but my question is how does it fit into a real world script. I dont see how arrays can be used, why not just declare a varible seperately. I am sure there is a good reason, i just dont see it. Please help. Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 10, 2006 Share Posted August 10, 2006 That is such an open-ended question. There are tons of very practical and efficient uses for arrays. Especially when dealing with very large scripts/applications....and on and on. Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 Im just looking for an example, I dont see how arrays are useful. Quote Link to comment Share on other sites More sharing options...
newb Posted August 10, 2006 Share Posted August 10, 2006 i use them everyday... Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 thats nice Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 10, 2006 Share Posted August 10, 2006 • When parsing data/html from external websites, and then looping through it to look for certain matches...then outputting only that which you want to use.• When retrieving data from the db (Simple queries) and then using while loops or whatever to display multiple rows.• When using explode functions to seperate words or characters in a string?We could do this all day....LOL. Arrays are very usefull in everyday coding. I can see how a newbie may not use them...if he is using PHP in the most basic/simplistic way. Quote Link to comment Share on other sites More sharing options...
corbin Posted August 10, 2006 Share Posted August 10, 2006 Hmmm an example... I'm not sure if you know what mysql is or if you have much experience but the most common way to return the results from a query is in an array (mysql_fetch_array, mysql_fetch_assoc)...Also $_POST and $_GET are arrays... SESSIONs are in an array... Cookies are as well... Theres end less uses for arrays... Quote Link to comment Share on other sites More sharing options...
newb Posted August 10, 2006 Share Posted August 10, 2006 lol Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 Lets say someone declares an array in there php code for the days of the week, like ["sunday","monday","tuesday"....] Whats the point of that. thats my question, because all the books i have been reading have been doing stuff like that, and i dont get the point. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 10, 2006 Share Posted August 10, 2006 it's an easy way of using one block of code to accomplish something. Using your example of an array with days of the week. If you coded:[code]$monday = "monday";$tuesday = "tuesday";etc;[/code]and you wanted to change each of those to upper case, you would have to do something like this:[code]$monday = strtoupper($monday);$tuesday = strtoupper($tuesday);etc for each variable[/code]if you had an array of days...[code]$days = array('monday', 'tuesday', etc...);[/code]you would only need to use a loop:[code]foreach ($days as $k => $day) { $days[$k] = strtoupper($day);}[/code]Three lines of code, as opposed to 7. And that's only applying one operation...imagine if you had to do several Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 10, 2006 Share Posted August 10, 2006 [quote author=draco2317 link=topic=103643.msg412828#msg412828 date=1155169954]Lets say someone declares an array in there php code for the days of the week, like ["sunday","monday","tuesday"....] Whats the point of that. thats my question, because all the books i have been reading have been doing stuff like that, and i dont get the point.[/quote]For one....when you get into using functions and classes....you don't want to declare 20 diferent $variable names, when you can declare one, and then utilize the values in the arrays globally accross the board, once you've made a call to the class. It makes for cleaner and more efficient code. Performance will also be better. Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 10, 2006 Share Posted August 10, 2006 [quote author=hitman6003 link=topic=103643.msg412834#msg412834 date=1155170213]it's an easy way of using one block of code to accomplish something. Using your example of an array with days of the week. If you coded:[code]$monday = "monday";$tuesday = "tuesday";etc;[/code]and you wanted to change each of those to upper case, you would have to do something like this:[code]$monday = strtoupper($monday);$tuesday = strtoupper($tuesday);etc for each variable[/code]if you had an array of days...[code]$days = array('monday', 'tuesday', etc...);[/code]you would only need to use a loop:[code]foreach ($days as $k => $day) { $days[$k] = strtoupper($day);}[/code]Three lines of code, as opposed to 7. And that's only applying one operation...imagine if you had to do several[/quote]Precisely. Good example. Quote Link to comment Share on other sites More sharing options...
ScottRiley Posted August 10, 2006 Share Posted August 10, 2006 They're probably just giving you an example of how to create arrays. For example, if you wanted to output a lot of information, lets say, years since 1800. Now, this isn't a perfect example, because there's a more efficient way to do this using math unctions, but lets say we had a list of a similar size. Now,, keeping the php as simple as we can, if you wanted to output these using normal php, we would first have to declare each year as a variable, here we would have 206 variables. If we then wanted to print these variables, we would need 206 print statements. However, if we pu these variables into an array with incrementing indicies, we would have 206 declarations, but we could easily eliminate the 206 print statements using a for loop:[code]<?phpfor($i=0;$<206;$i++){ print($array[$i];}?>[/code]obviously, this is not the most efficient example of how arrays save time, but it should give you an idea. Apologies for any errors in syntax, I was applying my previous knowledge of arrays o my rather weak PHP knowledge. Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 Okay that is starting to make sense, if i am understanding it correctly. Are you saying that the reasons to use arrays (but not all the reasons) is to have cleaner code as well as make it easier to make changes? Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 10, 2006 Share Posted August 10, 2006 [quote author=draco2317 link=topic=103643.msg412839#msg412839 date=1155170550]Okay that is starting to make sense, if i am understanding it correctly. Are you saying that the reasons to use arrays (but not all the reasons) is to have cleaner code as well as make it easier to make changes?[/quote]Just scratching the surface. Arrays can be used for many many diferent reasons. All of which, make performing certain tasks easier and yes, more efficient. Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 10, 2006 Share Posted August 10, 2006 [quote author=draco2317 link=topic=103643.msg412828#msg412828 date=1155169954]Lets say someone declares an array in there php code for the days of the week, like ["sunday","monday","tuesday"....] Whats the point of that. thats my question, because all the books i have been reading have been doing stuff like that, and i dont get the point.[/quote]Ok an example for you. Suppose you had the day of the week in a variable, $day. It holds the value 1 to 7, because there are seven days in a week. That's all you know, you just know that this variable holds a number from 1 to 7. You now need to print which day of the week that is. How would you do that?You would have an array, $days_of_week = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");Then to print the day you just echo $days_of_week[$day - 1];It's minus one because array elements are counted from zero.Pretty trivial but that's a very simple example of why we use arrays. Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 Thank you for everyone who is helping me understand, although i dont understand the code that king aurther posted, wouldnt it just print a specific day instead of the day it actually is? Quote Link to comment Share on other sites More sharing options...
king arthur Posted August 10, 2006 Share Posted August 10, 2006 It would print whichever day corresponded to the number in $day. If it was 1 it would print Monday, if it was 6 it would print Saturday. The point is not why there is a particular number in $day, the point is how to print the name of the day starting only with a number. Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 I understand Quote Link to comment Share on other sites More sharing options...
litebearer Posted August 10, 2006 Share Posted August 10, 2006 just to push it a little more...our families can be looked at as multi-dimensional arrays $a[lastnames here][firstnames here][gender here][age here] Quote Link to comment Share on other sites More sharing options...
draco2317 Posted August 10, 2006 Author Share Posted August 10, 2006 Anyone have an suggested readings for help with arrays Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 10, 2006 Share Posted August 10, 2006 Zend has a good intro to creating and manipulating arrays, as well as some of the array related functions, and some loops.http://devzone.zend.com/node/view/id/635 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.