View Full Version : The purpose of using arrays...

05-31-2004, 02:52 PM
Hi all,

After about 2 years of using/learning PHP, I have never once used arrays. I finally decided to read up on them - and after a couple of days of trying to a bit of research (light research at that) I don't really see a purpose to them.

I have had numerous people tell me that it can make life much easier - I am just yet to see it.

For example...

What would be the difference in doing this:

$apples[cost] = ("1.50", "2.00", "2.50");

as opposed to:

$apple1 = 1.50; $apple2 = 2.00; $apple3 = 2.50;

I know the first line is a lot less typing, but when recalling them I see $apple1 or $apple2 $apple3 quicker to type than $apples[cost][0] $apples[cost][1] and $apples[cost][2] - or have I completely misunderstood what I read.

Could someone perhaps point me in the direction of a basic tutorial for dummies regarding arrays - as so far I just don't get it - and how do they exactly make life easier.

Thanks in advance! :thumbsup:


05-31-2004, 03:05 PM
Think about if you want to store associated information to each apple.

$apple1 = 1.50; $apple2 = 2.00; $apple3 = 2.50;

Is workable until you want to add color and type to each then you are into arrays and why they are nice to have about.

$apple1[0] = 1.50;
$apple1[1] = "Delicious";
$apple1[2] = "green";

$apple2[0] = 2.00;
$apple2[1] = "granny smith";
$apple2[2] = "green";

$apple1['price'] = 2.50;
$apple1['type'] = "Washington";
$apple1['color'] = "red";

The following would make for some very complicated code in certain situations. You might have to break down and verify each use of the variable rather than just calling it by it's name.

$apple1_price = 1.50;
$apple1_type = "Delicious";
$apple1_color = "Green";

05-31-2004, 05:08 PM
I think the biggest advantage of arrays (and hashes) is iteration.

For example, it's much easier to do this...

for($i=0; $i<count($array);$i++) {
print "<p>".$array[$i]."</p>";

Than to do this...

print "<p>$array0</p>";
print "<p>$array1</p>";
print "<p>$array2</p>";
print "<p>$array3</p>";
print "<p>$array4</p>";
print "<p>$array5</p>";
print "<p>$array6</p>";
print "<p>$array7</p>";
print "<p>$array8</p>";
print "<p>$array9</p>";
print "<p>$array10</p>";
print "<p>$array11</p>";
print "<p>$array12</p>";

05-31-2004, 06:44 PM
iteration(as noted) & automation , bad example but ...

$cars = array(
array( 'make'=>'ferarri' , 'model'=>'GTO' , 'colour'=>'red' , 'price'=>'135,000' ) ,
array( 'make'=>'porsche' , 'model'=>'911' , 'colour'=>'black' , 'price'=>'99,000' ) ,
array( 'make'=>'jaguar' , 'model'=>'XJ12' , 'colour'=>'yellow' , 'price'=>'115,000' )


foreach($cars as $data){
$rets .= '
<td>'.$data['make'] .'</td>
<td>'. $data['model'] .'</td>
<td>'. $data['colour'] .'</td>
<td>'. $data['price'].'</td>
echo '<table>'.$rets.'</table>';

need that data stored somewhere ?

$_SESSION['cars'] = serialize( $cars );

need it back again ?

$cars = unserialize($_SESSION['cars']);

If you have been using PHP with a DB then you almost certainly have been using arrays already , $db_fetch_row($query) etc.

06-01-2004, 07:25 PM
I get it a little bit more, although anything shown so far I don't see much difference in without arrays... am I just being a dummy... cause I swear I'm quite intelligent most of the time! LOL

And firepages... I'm still waiting on a reply from a private message I sent ya! LOL :p

Need help on setting up a server here in down under if you have time - cannot seem to get help from optus or telstra!

Thanks in advance! :thumbsup:


06-02-2004, 12:47 AM
Do you know about 2D arrays?
That, I think, is where arrays get really powerfull.

06-02-2004, 01:07 AM
and how do they exactly make life easier.

$apples[cost] = array("1.50", "2.00", "2.50");

as opposed to:

$apple1 = 1.50; $apple2 = 2.00; $apple3 = 2.50;

using the array i can iterate through the array not knowing how many prices there are


echo $apples['cost'][$i]."<br>";

not matter how many prices i add the code works
where as if i tried to do the same with strings i could loop but
would have to hard code the loop value
or involve some other function


echo ${"apples$i"}."<br>";

just one example of how they make life a much more pleasent experience