Lost Password?


  #1 (permalink)  
Old 01-10-2007, 06:13 PM
clookid's Avatar   
clookid clookid is offline
Programmer
 
Join Date: Jan 2007
Posts: 148
Rep Power: 7
clookid is on a distinguished road
Default Arrays

Arrays

Arrays in PHP are pretty much different from any other implementation the author has seen in other languages. The concept of an array is simple - it's a list of items:

PHP Code:
  $my_array = array( "bob""fred""barney""thelma" ); 
Accessing the array is done as a whole ($my_array) or by array item. Items are numbered from 0 at the beginning, so $my_array[0] is "bob" and $my_array[3] is "thelma"

So far, this should be familiar to many coders. You don't have to declare arrays in PHP, or their length - the size of an array will adapt to meet the contents. Array items can be strings, numbers, even other arrays:

PHP Code:
  $my_array[4] = array("billy-anne""billy-bob""billy-sue"); 
A useful function to learn here is the print_r() function to show the structure and contents of any variable. You can use it to help you visualise how this data is arranged. You can also run PHP from the command-line if you have the php-cli program in your PHP installation package (from version 4.2.x onward). To run a script from the command line, use:

PHP Code:
  php-cli file.php 
The stops the HTTP headers from showing on the command-line, which keeps your screen tidy.

Try saving the following lines to a file then running it through PHP.

PHP Code:
<?
   $my_array 
= array( "bob""fred""barney""thelma" );
   
$my_array[4] = array("billy-anne""billy-bob""billy-sue");
    
   
print_r($my_array);
  
>
Now, the powerful and unique feature of PHP arrays (and also a big stumbling block for many) is that PHP arrays aren't just numbered, they're named. For good coding, you shouldn't assume that your arrays are numbered, or even numbered in order! This means that a traditional for( $i = 0; $i < count($my_array); $i++) loop won't necessarily work.

Named arrays are assigned slightly differently, but not much - you can use this format for numbered arrays too:

PHP Code:
   $this_user = array( "firstname" => "Bob",
                       
"surname"   => "Smith",
                       
"age"       => 24 );
   
$this_user['firstname'] = "Robert"
Hopefully you can see from this example why named arrays are useful. Later on in the database section we show how you can retrieve rows from a database query and store each row as a named array. Identifying in your HTML code what $row[4] is can be annoying and time-consuming, because you have to go back and find the SQL statement and count to the 5th field... etc. But if you see $row["age"] you know exactly what part of the data is being shown.

(Un)fortunately it doesn't end there with arrays - you can treat them like a stack and push things onto, or pop them off of the top, like some programmers would with assembler or forth. PHP also lets you shift and unshift items to/from the bottom of the stack too. And because you can store arrays in arrays, you can simulate trees. If you're not a full programmer yet and this means nothing to you.. good! Less work for me :-)

Read the manual's function reference - it has a whole section of array functions that let you shuffle, reverse, sort, splice, count, merge, and filter arrays. You can subtract one array from another, or build an array of the unique values of another array.. in short, you can do a helluva lot with arrays. They're one of the most powerful features of PHP.

I'll cover some simple array functions (like looping over the contents) and ask you to play with the manual a little.

This tutorial was written by another one of my friends, if you would like to use this tutorial please send me a PM

Last edited by clookid; 01-11-2007 at 12:55 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-11-2007, 09:30 PM
xtraze xtraze is offline
Programming God
 
Join Date: Dec 2006
Location: Sri lanka
Posts: 921
Rep Power: 0
xtraze is on a distinguished road
Send a message via MSN to xtraze Send a message via Skype™ to xtraze
Default

Nice tutorial man,
Exciting, now I think I'm learning php as well coz I only knew HTML,XHTML
DHTML a little, I saw another thread saying how to connect to a database, I think both are linked, (tutorials).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python arrays... Sir_Rimo Python 3 06-20-2007 09:54 AM
variable length arrays in C rattlepanos C and C++ 3 05-03-2007 12:35 PM
Are arrays sort of like matrices? Sionofdarkness Java Help 5 08-21-2006 03:30 PM
Arrays Sionofdarkness C and C++ 5 07-26-2006 06:35 PM
Arrays in NET 2.0 or CLI Managed C++ Void Managed C++ 1 07-18-2006 08:57 PM


All times are GMT -5. The time now is 12:40 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads