Best way to learn arrays?

Hi guys, been trying to get this to work:

$checkwishlist = mysqli_query($link, "SELECT moviename FROM movies WHERE wishlist='1' AND userid='$userid'");
	$result = mysqli_num_rows($checkwishlist);
		if (!$result)
			{
				$wishlist = '0';
			}
		else
			{
				$wishlist = '1';
				$wishlist = mysqli_query($link, "SELECT movies.moviename, genres.genre_abbreviation FROM movies INNER JOIN genres ON movies.genreid=genres.id AND movies.wishlist=1 AND movies.userid='$userid'");
				while ($row = mysqli_fetch_assoc($wishlist)) {
                           NO IDEA HOW TO HANDLE THIS PART
			}

then in my display .php page I have:


if ($wishlist == '1')
{
echo 'test';
}

that part works fine and the code above it to handle $wishlist works great too. ‘test’ obviously just there to make sure it works. but I want to display movies that are tagged ‘1’ in the wishlist column like so:

movie genre
movie genre
movie genre

Anyone have any good tips on how to learn arrays for this kind of stuff? This is where I always get stuck: displaying my information/stats etc. and it’s becoming very frustrating. :frowning:

woo worked this one out for myself, stuck with this code:

$checkwishlist = mysqli_query($link, "SELECT moviename FROM movies WHERE wishlist='1' AND userid='$userid'");
	$result = mysqli_num_rows($checkwishlist);
		if (!$result)
			{
				$wishlist = '0';
			}
		else
			{
				$wishlist = '1';
}

and used the following code for my output, works great

if ($wishlist == '1')
{
				$wishlist = mysqli_query($link, "SELECT movies.moviename, genres.genre_abbreviation FROM movies INNER JOIN genres ON movies.genreid=genres.id AND movies.wishlist=1 AND movies.userid='$userid'");
				while ($row = mysqli_fetch_assoc($wishlist)) {
				echo '<div id="wishlistmovie">';
				echo $row["moviename"];
				echo '</div>';
				echo '<div id="wishlistgenre">';
				echo $row["genre_abbreviation"];
				echo '</div>';
				}
}

woo, kinda happy with myself heh. hope it’s acceptable code! now to figure out how to get the output lined up/styled correctly.

still, if anyone can recommend a good book/learning resource for arrays and onward in PHP, that’d be great. already have kevin yank’s book.

I had a look earlier to see if I can help but this is a bit above me.

I recommend trying itunes. They have a free section called “ItunesU”. If you type in php there are some recorded live university lectures that have been really helpful. One of them also sets coursework so it’s good to try some of the things they do.

Sitepoint forum is where I’ve learnt alot though, I think you might have just got unlucky with this post.

Iwould make it


while ($row = mysqli_fetch_assoc($wishlist)) {
echo \
.'<div id="wishlistmovie">'.
       $row["moviename"].
       '</div>'.
       '<div id="wishlistgenre">'.
       $row["genre_abbreviation"].
       '</div>';
 }

this does the exact same thing, just cleaner. The php code here is stripping out characters,it should be “echo
.” versus “echo n.” Have you read the manual? Tizag is also pretty good http://www.tizag.com/phpT/

Yep, I remember just how frustrating it is.

Understanding arrays is a cornerstone of programming.

PHP has more than 80 array functions.

Some of them you will use daily, some monthly, some once in a blue moon.

But you should be roughly aware of what each of them does, you don’t need to remember exactly how some of the more esoteric ones work exactly, but know that they are there.

So to answer the question in the title of your post “Best way to learn arrays?”, I submit this to you.

Take 2 or 3 days off, and teach yourself nothing else but arrays from the PHP manual.

Create a folder on your computer called localhost/arrays/ and for each function in the manual type out an imaginary list ( I used peoples names queuing up in a post office, in my mind) and apply the target function to that list of people, and eventually their attributes as you move into multidimensional arrays.

Then do this:
Get the array basics clear in your head.

Then look at individual array functions PHP: Array Functions - Manual

Figure out what it is to iterate through and array with key, current, next, end, prev, reset (old skool fundamentals, apply to most other languages)

Then look at the 11 sorting functions, and shuffle

Then look at the difference between stacks, queues and sets: pop, push, shift and unshift

Then look at the tools for sets of arrays, array_diff, array_merge, array_intersect_* then look at the ones you missed array_map etc.

Come back and ask any question about any array function you have on here, and you will get lots of help, but if you want to get serious about PHP you have to get serious about arrays. Your original question implies you are, hence my serious answer, not just a fix to your immediate problem.

You will thank me.

I thank you already Cups and I’m sure I will again. I will take your advice. Really helps having a plan laid out to learn from, and what you have there sounds really good for the way my brain works.

The sitepoint forums are definitely great. I haven’t come across a better site to actually get helpful tips from. Have learned a lot here already.

Thanks rguy also, I will try that out.

I’m glad you appreciate my response. It is borne of recognising your plea from the wasted years I spent dithering around with arrays and not really understanding the power it puts into your hands.

What is not helpful on the otherwise superb PHP manual is that it lumps all the array functions together alphabetically - and not in groups as I briefly described for you.

I got those groups from the excellent (and cheap) “Zend PHP5 Certification Guide” which gives a lightweight but effective learning guide to PHP, and it really, really filled all the gaps in for me. I cannot recommend it any more highly. It is a thin book and freely admits you will spend 2 minutes on a page in there, and 20-30 minutes in the corresponding pages in the manual.

When researching arrays I was very surprised to be unable to find a good enough tutorial that dealt with all of the PHPs array functions.