Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

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.
Amazing Font Pack!

Featured Tutorialsmore

Signature Techniques
Signature Techniques
- Adobe Photoshop -
Displacement Maps
Displacement Maps
- Adobe Photoshop -
Simple Shape Tween...
Simple Shape Tween...
- Macromedia Flash -
The Pattern Maker
The Pattern Maker
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 867 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1037 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1042 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1919 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1816 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14417 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2712 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2752 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3427 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2344 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2482 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2792 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673