I have never really understood why people have problems with arrays.  Just
think of an array as a table of values.  A one-dimensional array would be
a table with a single column and each row has a number (or index)
assigned to it:

  +---+--------+
  | 0 | Apple  |
  +---+--------+
  | 1 | Orange |
  +---+--------+
  | 2 | Banana |
  +---+--------+

In PHP this simple array could br created like this:

  $fruits[0] = 'Apple';
  $fruits[1] = 'Orange';
  $fruits[2] = 'Banana';

Or there is a shortcut:

  $fruits = array('Apple','Orange','Banana');

Now, the row numbers (or indices) do not have to start at 0.

  $fruits[7] = 'Apple';
  $fruits[10] = 'Orange';
  $fruits['foo'] = 'Banana';

Would create a table like this:

  +-----+--------+
  | 7   | Apple  |
  +-----+--------+
  | 10  | Orange |
  +-----+--------+
  | foo | Banana |
  +-----+--------+

As you can see, row names don't even have to be numbers.  When using
non-numerical array indices like this the array is sometimes referred to
as an associative array, although in PHP we don't really differentiate.

The shortcut for creating this array where you name each row would be:

  $fruits = array(7=>'Apple', 10=>'Orange', 'foo'=>'Banana');

and obviously, to display an element you would do:

  echo $fruits['foo'];

To loop through and display every value in the array you can use foreach:

  foreach($fruits as $value) echo $value;

If you also want to display the row name (also known as the index or the
key):

  foreach($fruits as $key=>$value) echo "$key: $value";

Now, what really seems to confuse people are multi-dimensional arrays, but
they are exactly the same.  A multi-dimensional array can be thought of as
a table with more columns and you give each column a name as well now.
Just like a tic-tac-toe board:

          0        1
  +---+--------+-------+
  | 0 | Apple  | 1.99  |
  +---+--------+-------+
  | 1 | Orange | 2.99  |
  +---+--------+-------+
  | 2 | Banana | 0.89  |
  +---+--------+-------+

So the Apple element would be in position 0,0, Orange would be 1,0 and
0.89 would be in position 2,1.  Or in PHP you would write this as:

  $fruits[0][0] = 'Apple';
  $fruits[0][1] = 1.99;
  $fruits[1][0] = 'Orange';
  $fruits[1][1] = 2.99;
  $fruits[2][0] = 'Banana';
  $fruits[2][1] = 0.89;

That's of course a bit wordy.  If you think of this slightly differently,
consider that each row is actually a small array of two items.  The fruit
and the price.  So you could also write this as:

  $fruits[0] = array('Apple',1.99);
  $fruits[1] = array('Orange',2.99);
  $fruits[2] = array('Banana',0.89);

And then further breaking it down, we see that $fruits is just an array of
arrays, so it could actually be written as:

  $fruits = array(array('Apple',1.99), array('Orange',2.99), array('Banana',0.89));

and again, you don't have to use simple numbers to name your rows and
columns.

         Name    Price
  +---+--------+-------+
  | a | Apple  | 1.99  |
  +---+--------+-------+
  | b | Orange | 2.99  |
  +---+--------+-------+
  | c | Banana | 0.89  |
  +---+--------+-------+

  $fruits = array( 'a'=>array('Name'=>'Apple', 'Price'=>1.99),
                   'b'=>array('Name'=>'Orange','Price'=>2.99),
                   'c'=>array('Name'=>'Banana','Price'=>0.89) );

Looping through a multi-dimensional array is a little trickier:

 foreach($fruits as $k=>$arr) {
    echo "$k: ";
    foreach($arr as $kk=>$val) {
       echo "$kk: $val ";
    }
    echo "<br>\n";
 }

If you run that code on the $fruits array you will get this output:

a: Name: Apple Price: 1.99
b: Name: Orange Price: 2.99
c: Name: Banana Price: 0.89

So basically you loop through all the rows and then inside each row
iteration you loop through each column.

This is about as complex as you are ever likely to get.  You may end up
with 3-dimensional or higher arrays, but there really is no difference.
The display loop will of course get more complex as you will need another
level of looping.  For a 3-dimensional array you would have items like
$fruits[0][0][0] and your display loop would be:

  foreach() {
    foreach() {
      foreach() { }
    }
  }

-Rasmus

 On Sat, 11 May 2002, r wrote:

> Greetings people,
> Special greetings to all of you who have helped me in the past.
>
> As most of you know i am a newbie, I learned a bit of PHP via webmonkey and
> a few other places, seeing the power of PHP i decided to convert from Java
> servlets and JSP (JSP coz its expensive to host and not many hosting
> opportunities) so I baught a book called "The PHP black book".
> Anyway, now that the background is done heres my questions:
>
> 1)How many of you have seriously dug into arrays and has it been important
> in your programming?
> 1.1)Do you think you could have done the same thing you did with arrays
> WITHOUT arrays?
> (The reason i ask this is theres a whole chapter dedicated to arrays in the
> book & its pretty frustrating)
> Last question:
> Is ther any function to make the program "sleep" for 10 seconds or so? or
> does anybody have a function that does this?
>
> ANY replies.... good,bad,flames will be welcome.
> Cheers,
> -Ryan.
>
> /* You cannot get to the top by sitting on your bottom. */
>
>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to