cnicollonline

"I was just guessing at numbers and figures, pulling the puzzles apart"

Way back when I started programming there were two things I just could not wrap my head around. Unfortunately they fell hand in hand which made doing an alternative quote tedious. Loops and arrays were nightmares. This post is outlines a few small pointers I’ve picked up along the way.

What is an array, what is a loop?

The best way to think of an array is as a container, or box. The box (array) can hold values and objects for safe keeping, we can put things into the box, and we can take them out. We do this by using a loop. The loop will cycle through the array, and within the loop we can perform actions to all the objects, a group of objects or any single object.

There are three types of loops are the for loop, the foreach, and the while loop. What loop to use and when I belive comes with experience and practice. The three loops are similar in what they do, but depending when you use them, or how they’re used is the tricky part.

Defining an Array

I stated earlier that an array was simply a collection of values or objects.

$fruits = array("apples", "oranges", "pears", "cherries");

Thats all there is to an array, I have it stored within a variable that acts as the container. Now how do we access these delicious fruits?

echo $fruits[0];
echo '<br>';
echo $fruits[1];
echo '<br>';
echo $fruits[2];
echo '<br>';
echo $fruits[3];

NOTE:This is not a loop but its a wonderful method to quickly print out all the elements within an array. I use this a lot when developing and debugging.

// print array
echo '<pre>';
	print_r($fruits); // print array
echo '</pre>';

outputs:

Array
(
    [0] => apples
    [1] => oranges
    [2] => pears
    [3] => cherries
)

What makes an array? You can see from the output above that there are two elements within the array [key]=>[value]. Each array element is assigned a unique key. The key is how we can call any given member within the array.

How do we add a new fruit? To add something to our array we call the array, and give it a new value in an empty index position (php will do the math for you)

$fruits[] = 'grapes';
$fruits[] = 'mango';

printing the array again we can now see that indeed our fruits have been added.

Array
(
    [0] => apples
    [1] => oranges
    [2] => pears
    [3] => cherries
    [4] => grapes
    [5] => mango
)

Working with loops

You might be asking yourself “Well how is that any easier then writing it out?”. Thats when loops become a handy tool, we can simply call any of the three loops to loop through the array and print everything within the array (opposed to printing them individually using $fruits[x])

foreach loop: Perhaps as far as this demo goes the minimalist and simplest loop. It basically iterates over each element and outputs its value.

// foreach
foreach ($fruits as $loop) {
	echo $loop;
	echo '<br>';
}
echo '<hr>';

The next two loops are similar in what they do. They both allow for more control within the loop allowing you to perform actions based on conditional statements.

for loop

// for loop
for ($i=0; $i<sizeOf($fruits); $i++) {
	echo $fruits[$i];
	echo '<br>';
}
echo '<hr>';

while loop

// while loop
$count = 0;
while ($count < sizeOf($fruits)) {
	echo $fruits[$count];
	echo '<br>';
	$count++;
}

Theres so much you can do with loops and still I struggle on getting things done with them. I wanted to keep this post simple, there are still multidimensional arrays which add a whole layer of complexcity to the topic. Hopefully I’ll continue this topic soon.

Heres the full script

<?php

// fruit array
$fruits = array("apples", "oranges", "pears", "cherries");

// getting the fruit
echo $fruits[0];
echo '<br>';
echo $fruits[1];
echo '<br>';
echo $fruits[2];
echo '<br>';
echo $fruits[3];

// print array
echo '<pre>';
	print_r($fruits); // print array
echo '</pre>';

// add to the array
$fruits[] = 'grapes';
$fruits[] = 'mango';

// print array
echo '<pre>';
	print_r($fruits); // print array
echo '</pre>';

// ================================================= LOOPS =================================================
echo '<hr>';

// foreach
foreach ($fruits as $loop) {
	echo $loop;
	echo '<br>';
}
echo '<hr>';

// for loop
for ($i=0; $i<sizeOf($fruits); $i++) {
	echo $fruits[$i];
	echo '<br>';
}
echo '<hr>';

// while loop
$count = 0;
while ($count < sizeOf($fruits)) {
	echo $fruits[$count];
	echo '<br>';
	$count++;
}
?>

Write a Comment