Jump to content


Photo

Array question


  • Please log in to reply
21 replies to this topic

#1 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 August 2006 - 11:43 PM

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.

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 10 August 2006 - 12:11 AM

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.
PHP Ninja

#3 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:14 AM

Im just looking for an example, I dont see how arrays are useful.

#4 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 August 2006 - 12:14 AM

i use them everyday...

#5 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:20 AM

thats nice

#6 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 10 August 2006 - 12:21 AM

• 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.
PHP Ninja

#7 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 10 August 2006 - 12:22 AM

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...
Why doesn't anyone ever say hi, hey, or whad up world?

#8 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 10 August 2006 - 12:26 AM

lol

#9 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:32 AM

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.

#10 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 10 August 2006 - 12:36 AM

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:

$monday = "monday";
$tuesday = "tuesday";
etc;

and you wanted to change each of those to upper case, you would have to do something like this:

$monday = strtoupper($monday);
$tuesday = strtoupper($tuesday);
etc for each variable

if you had an array of days...
$days = array('monday', 'tuesday', etc...);

you would only need to use a loop:
foreach ($days as $k => $day) {
  $days[$k] = strtoupper($day);
}

Three lines of code, as opposed to 7.  And that's only applying one operation...imagine if you had to do several

#11 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 10 August 2006 - 12:37 AM

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.


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.

PHP Ninja

#12 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 10 August 2006 - 12:38 AM

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:

$monday = "monday";
$tuesday = "tuesday";
etc;

and you wanted to change each of those to upper case, you would have to do something like this:

$monday = strtoupper($monday);
$tuesday = strtoupper($tuesday);
etc for each variable

if you had an array of days...
$days = array('monday', 'tuesday', etc...);

you would only need to use a loop:
foreach ($days as $k => $day) {
  $days[$k] = strtoupper($day);
}

Three lines of code, as opposed to 7.  And that's only applying one operation...imagine if you had to do several


Precisely. Good example.
PHP Ninja

#13 ScottRiley

ScottRiley
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 10 August 2006 - 12:41 AM

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:
<?php
for($i=0;$<206;$i++)
{
    print($array[$i];
}
?>

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.

#14 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:42 AM

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?

#15 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 10 August 2006 - 12:43 AM

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?


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.
PHP Ninja

#16 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 10 August 2006 - 12:45 AM

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.


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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#17 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:51 AM

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?

#18 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 10 August 2006 - 12:57 AM

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#19 draco2317

draco2317
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 12:58 AM

I understand

#20 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 10 August 2006 - 01:12 AM

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]


all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users