Welcome, Guest

Please login or register

TUTORIALS SUBMENU ---->

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING

Related Links

Using Arrays (with EXPLODE)


Arrays are a collections of data, grouped into one variable, along a variety of streams. For example, I might have 6 names in one array, and to access the names I'll use $names[0], $names[1], etc. This function is often used with the FOR and FOREACH functions, but as this tutorial is written for beginners, I'll stick to using the WHILE() statement instead.

A Simple Array
For our first example, lets imagine that we have a class of 10 students at a school, and need to put all of their names into an array. In our code below we will input all their names sequentially:

<? 
$class = "Hellview School, Class 10SATAN 2004.";
$names = array (
     "anna",
     "emma",
     "robbie",
     "evan",
     "lewis",
     "simon",
     "nicole",
     "toby",
     "caroline",
     "satan"
);
?>

We now have all of the names of the pupils in our class in an array called $names. If we want to output these values sequentially, all we need to do is PRINT the variable out as follows:

<? 
print_r($names);
?>

This is, of course, very exciting, but what if we want to print out the first and last names in the array only?   Well, the beauty of arrays is that you can do this with absolute ease, as this snippet of code reveals:

<?
 echo 'First name: ' . $names[0] . '<br>Last name: ' . $names[9];
 ?>

Note: Remember that with arrays, the first entry isn't 1... its 0!   So if we want to output the first value we will specify $names[0].

Advanced Array Instructions
Ok, that was fairly simple. Now I’ll show you how to loop through each stream, outputting each value as you go (but, unlike simply PRINTing it, this method will let you process the results a little). Our first two variables in the example below tell the WHILE loop where to start and finish. The $finish variable counts how many streams there are in the array.

<?
$start = 0;
$finish = count($names);

while ( $start <= $finish )
{
echo 'Position :' . $start . ' has the name <b>' . $names[$start] . '</b> in it.<br>';
$start = $start + 1;
}

echo 'Finished outputting names for the class: ' . $class;
?>

Another important function when using arrays, especially for dynamically created arrays, is EXPLODE(). This function will take a separator that you define, and go through a string, dividing it into parts appropriately. For example, if I had a simple string variable that had “anna,emma,robbie,evan” in it, I could use EXPLODE inside a loop control structure, to output these values into an array. This is the basis of how dynamic arrays work. Here's an example of the EXPLODE() function at work:

<?php
$string = "anna,emma,robbie,evan";
$exploded = explode (",", $string);

echo '$string is now an array.<br>';
$start = 0;
$finish = count($exploded);

while ( $start < $finish )
{
echo 'Position :' . $start . ' has the name <b>' . $exploded[$start] . '</b> in it.<br>';
$start = $start + 1;
}

echo 'Finished.';
?>
 

So, as you can see, you can use anything as a separator, whether it be a comma, or small unique string, or a space, anything! It could even be a sentence or a whole story! The possibilities are endless.  I hope this tutorial has helped you along with arrays and slightly with EXPLODE(). Just keep in mind I only outlined the explode() function, it wasn’t meant to be an in-depth tutorial, and there are additional quirks to it if you want to research them further. Please don’t hesitate to contact me about this tutorial if you need to, click on my name below which will refer you to the Creative Forums, where you can Private Message or Email me. You may be required to sign up.

- Tutorial written by Scrowler

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Featured Content

Retro Style Text
Retro Style Text
- Adobe Photoshop -
Abstract Wireframes
Abstract Wireframes
- Adobe Photoshop -
Simple Sound Contr...
Simple Sound Contr...
- Macromedia Flash -
Pixel Stretch
Pixel Stretch
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Special Options
Printer Friendly Version
Forum Threads

Competition Discussion - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:48pm
Activity: 0 replies, 53 views
 Competition - Brushes
Author: Man1c M0g
Posted: Feb 07th, 5:46pm
Activity: 0 replies, 54 views
 PM Spamming
Author: Tamlin
Posted: Feb 06th, 1:24pm
Activity: 7 replies, 115 views
Vector Clipart Bank
Author: Crapoun
Posted: Feb 06th, 11:29am
Activity: 2 replies, 93 views
How did ...
Author: MoodsR4Cattle
Posted: Feb 05th, 6:09pm
Activity: 6 replies, 26 views
Tips and trick for Texturing/Materials
Author: noorjan
Posted: Feb 05th, 4:59am
Activity: 2 replies, 108 views
 A Billion Styles - Please Help Me!!
Author: Angelz
Posted: Feb 03rd, 6:36pm
Activity: 2 replies, 133 views
101 Things you didnt know in 3DS Max ...in fact...
Author: noorjan
Posted: Jan 31st, 6:04pm
Activity: 0 replies, 160 views
Pee Wee get's an IPad
Author: MoodsR4Cattle
Posted: Jan 30th, 4:25pm
Activity: 2 replies, 163 views
Spam :: Online hotel reservations for Hotels in...
Author: kieulinh
Posted: Jan 28th, 6:39am
Activity: 0 replies, 204 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 3 replies, 17 views
New Design
Author: unleash
Posted: Jan 23rd, 12:39am
Activity: 27 replies, 727 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    415