WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 4107
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.









Array Manipulation With PHP
Categories : PHP, Arrays
10 PHP Functions I Bet You Didn`t Know About
Categories : PHP, PHP Functions, Filesystem, Arrays, Errors and Logging
Sockets and PHP
Categories : PHP, Sockets
PHP and PostgreSQL
Categories : PHP, Databases, PostgreSQL
Static HTML Generation With PHP
Categories : PHP, HTML and PHP
Date Arithmetic With MySQL
Categories : PHP, Databases, MySQL, Date Time
Executing SQL Server Stored Procedures With PHP
Categories : PHP, Databases, MS SQL Server
Time Is Money Part 1 of 2 - Designing and implementing a Web-based application
Categories : PHP, Databases, MySQL, Complete Programs
Using the .NET Assembly in PHP
Categories : PHP, .NET
PHP Debugging Tutorial
Categories : PHP, Debugging
Keep your Banner Waving
Categories : PHP, PHP Functions, Miscellaneous, Site Planning, Utilities
Using Adobe's Flex Builder tool to connect a PHP backend to the front end
Categories : PHP, Macromedia Flex
PHP and MySQL News with Comments
Categories : PHP, Databases, MySQL
Building a WAP site using PHP3 and MySQL
Categories : PHP, MySQL, WML, WAP
User identification using cookies in PHP and MySQL
Categories : PHP, Databases, MySQL, Cookies