WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : An Overview of Arrays in PHP
Categories : PHP, Arrays
codewalkers
codewalkers
Date : 2003-02-02
Grade : 0 of 5 (graded 0 times)
Viewed : 2485
Search : More Articles by codewalkers
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


Arrays in PHP

The PHP Manual defines an array in PHP as an ordered map. Personally, I don't like this definition. I will define an array in PHP as a collection of variables, which I think is a little more descriptive. In most languages, atleast the ones I am familar with, the variables in this collection must all be of the same type. Not so in PHP! This is because of PHP's Type Juggling. I'm not going to go into great detail about Type Juggling, but basically it means that you don't declare a variable as a certain type. The context in which the variable is used determines its type.

So, how do you assign values to arrays? The exact same way as you would any other variable, except that you specify which element of the array you wish to work with. One important thing to note is that, by default, arrays start numbering their elements at zero. Check this out:


<?PHP
$array
[0] = 3;
$array[1] = 6;
$array[2] = 2;
?>


A cool feature of PHP is the ability to use associative arrays. Associative arrays give you the ability to name your array's keys. So, instead of having $array[0] and $array[1] you can have $array['something'] and $array['anotherthing'].

So what's the big deal? Why would I want to use arrays rather than just regular old variables? Imagine this: You are working with customer's names. You have 5 of them that you continually need to echo out. With regular variables, you would do it like this:


<?PHP
echo "$customer1<BR>\n";
echo
"$customer2<BR>\n";
echo
"$customer3<BR>\n";
echo
"$customer4<BR>\n";
echo
"$customer5<BR>\n";
?>




Pretty long winded. Imagine doing that with 100 customers. Or 1000. With arrays, you can use a foreach loop. What a foreach loop does is loop as many times as you have elements in an array. Suppose you had an array called $customer that contained your customer names. Check this out:


<?PHP
foreach($customer as $value) {
echo
"$value<BR>\n";
}
?>


Now how easy is that? That code will not change if you have 100 or 1000 customers. That's the beauty of arrays.

Ok, that clues you in on some simple arrays. What about multi-dimensional arrays? What are they? Let's take a look at a two-dimensional array as an example. Say you are dealing with selling books. You want to place the names and prices of these books into an array. What you would want to do is create an array where each element is an array. Sound confusing? It really isn't. Take a look at this example:


<?PHP
$books
= array(0=>array('name'='A Book','price'=>9.99),1=>array('name'='Another Book','price'=>17.99));
?>


Now, here are a couple ways to access that data in our two-dimensional array:


<?PHP
echo $books[0]['name'];
echo
$books[1]['price'];
$books[0]['price'] = 12.99;

foreach(
$books as $onebook) {
echo
$onebook['name'] . " sells for $". $onebook['price'] . "<BR>\n";
}
?>


There you have it. A simple introduction to arrays in PHP. I hope it was helpful to you. For more info, check out the PHP Manual. There are a whole bunch of functions in there that can be used with arrays.









10 PHP Functions I Bet You Didn't Know About!
Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging
Array Manipulation With PHP
Categories : PHP, Arrays
Working with Dates and Times in PHP
Categories : PHP, Date Time
Date Arithmetic With MySQL
Categories : PHP, Databases, MySQL, Date Time
Using the .NET Assembly in PHP
Categories : PHP, .NET
Aspect-Oriented Programming and PHP
Categories : PHP, Aspect Oriented Programming
Saving Images in MySQL
Categories : MySQL, PHP, Graphics, Databases
PHP References Explained
Categories : PHP References, PHP
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Logging with PHP
Categories : PHP, Log Files
Building A Persistent Shopping Cart With PHP and MySQL
Categories : PHP, MySQL, Databases, Ecommerce
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
Generating One-Time URLs with PHP
Categories : PHP, URLs
Creating Auto-incrementing ID Fields with PHP and Oracle
Categories : PHP, PHP options/info, Databases, Oracle
Data, its presentation and user interface forms
Categories : PHP, XML, User Interface