Jump to content

Array question


draco2317

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.