On Mon, Mar 16, 2009 at 03:46:29PM -0500, PJ wrote:


> >> $book_publisher = array();
> >> $SQL = "SELECT id, bookID, publisher_id FROM book_publisher";
> >> if ( ( $results = mysql_query($SQL, $db) ) !== false ) {
> >> while ( $row = mysql_fetch_assoc($results) ) {
> >> $book_publisher[$row['bookID']][] = $row['publisher_id'];
> Could you explain this, please - this is further from me than Arcturus:
> $book_publisher[$row['bookID']][] = $row['publisher_id'];
> What is purpose and meaning of all the [] brackets? Especially the empty
> ones?
> I following your example, I have been able to finally get all the arrays
> in place. Only now, I am looking into how to retrieve the parts of the
> array...

PJ, you *really* need to read up on the *basics* of the PHP language.
Please buy a good book on the subject, or search php.net for "arrays"
and read and understand what is there.

To summarize, $book_publisher is an array, as you can tell by the
square brackets after it. Its first index is book IDs. So in the
expression $book_publisher[$row['bookID']], $row['bookID'] is the
"index" for one of the members of the array. It could be 5 instead, as
in: $book_publisher[5]. 

Likewise, $row is an array. When you see an expression like
$array['something'] or $array[1], the value being represented is
actually the value at that index of the array. If you have an array of
letters called "$alphabet", it might look something like this:

indexes:  'a', 'b', 'c', 'd', 'e'
contents: 'alfa', 'bravo', 'charlie', 'delta', 'echo'

So if you had an expression like: $alphabet['d'], it would actually
represent the value 'delta'. Read the above until that's clear.

In the case of $row['bookID'], it's just a value at the 'bookID' index
of the $row array. It could be '1234', '55-345' or whatever.

So $book_publisher[$row['bookID']] is the array member at the
$row['bookID'] index of the array.

Lastly, since an array member can also be another array, you can have
"multidimensional arrays". In your case, it's:


Notice the last set of open and close backets on the end. That tells you
that $book_publisher[$row['bookID']] is an array member which is
actually another array. But empty open/close brackets have a special
meaning. You will only find them (meaningfully) on the left hand side of
an equation. They mean that you're adding whatever is on the right side
of the equation to the last position in the array. So

$book_publisher[$row['bookID']][] = $row['publisher_id']

Says that the $row['bookID'] member of $book_publisher is an array, and
that you're adding a member on the end whose value is

Depending on your mental abilities, you may have to draw the arrays on a
sheet of paper to make it make sense. Also remember that arrays can be
indexed either numerically or "associatively" or both. A numerically
indexed arrays would have just numbers for the indexes. An associatively
indexed array is what you have above, indexed not necessarily by number
but possibly by strings or whatever.

All this is fundamental to array processing under PHP and used
constantly. So learn it and learn it well. If my explanation is
insufficient, then look it up and fully understand it before you go on.
Otherwise, you'll take one step, ask a bunch of questions, take another
step, ask a bunch of questions, and generally make yourself a nuisance.
You're essentially asking professional programmers to tutor you for free
when you really should take the initiative and read/study the subject
yourself. It doesn't matter if you don't really want to *be* a
professional programmer at the end of this. You have to approach it as
though that's your goal.

Let me put this another way. Assume you're a professional chef. You get
paid lots of money to cook food for people. You've had years of
education and practice and get paid accordingly. Suddenly one day,
management sends you some wet-behind-the-ears kid who's flipped burgers
at McDonalds, and you're supposed to teach him how to cook like you do.
Every time he cuts a shallot or dices an onion, he asks you a bunch of
silly questions he should know if he had gone to culinary school like
you did. You're busy. And the questions are things like "What's the
difference between a teaspoon and a tablespoon?". "How come I have to
dice onions? Can't I just throw them in the pot?" And you know that the
only reason the kid is there is so he can learn to cook some stuff to
impress his girlfriend, who's gonna dump him in three months anyway.
Think about it.

Paul M. Foster

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

Reply via email to