Jump to content

Archived

This topic is now archived and is closed to further replies.

draco2317

Array question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
• 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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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]<?php
for($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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.