+ Reply to Thread
Results 1 to 2 of 2

Thread: Arrays

  1. #1
    clookid's Avatar
    clookid is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0

    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:

    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:

    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:

    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.

    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:

    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-10-2007 at 09:55 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    xtraze is offline Programming God
    Join Date
    Dec 2006
    Location
    Sri lanka
    Posts
    911
    Rep Power
    0
    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).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. Need Help on arrays and linking to arrays
    By Trendnet18 in forum Java Help
    Replies: 15
    Last Post: 01-19-2010, 11:56 AM
  3. Arrays
    By hoku2000_99 in forum C and C++
    Replies: 2
    Last Post: 03-24-2009, 09:23 AM
  4. Js, XML, and arrays
    By domestic in forum JavaScript and CSS
    Replies: 4
    Last Post: 02-27-2008, 02:49 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts