+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP Arrays

  1. #1
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    PHP Arrays

    Arrays

    Arrays are like a container to hold several variables. They are usually the first data structure taught to students. This is because an array is essentially the underlying data structure to the more complicated data structures such as the linked list, vector, and hash tables.

    When you purchase a book on algorithms, it probably mentions something about data structures as well. The truth is data structures and algorithms go hand in hand. If you choose the wrong data structure for a problem, you may have a hard time getting a good implementation of your algorithm.

    This tutorial is about 1-dimensional arrays in PHP and some of the functions for working with arrays.

    As usual I will link to some problems for you to work on if you choose to. The best way to get good at programming is to write programs.

    Topics

    Defining Arrays
    Walking The Array
    Associative Arrays
    Returning Arrays
    Array Functions
    In Array
    Sorting
    --------------

    Defining Arrays

    You can define an array in one of two ways. Using the array() function or the array operator ([]).

    An array has a key and a value. So an array can easily function as a hashtable, and they are used to create hash tables.

    Examples of defining arrays:

    Code:
    $names = array("James","John","Robin","Sarah");

    $names[] = "James";
    $names[] = "John";
    $names[] = "Robin"
    When you use the [] operator, the element is added to the next available location. You can also add to the array by including a subscript of where you want to add the element.

    Example:

    Code:
    $names[5] = "James"
    Walking the Array

    Since arrays use incremented subscripts you can use a loop to "walk through the array".

    Example:

    Code:
    for ($i=0;$i<count($names);$i++) {
        echo 
    "$names[$i]<br />";

    A few things to note here:
    1. The first subscript of the array is 0.
    2. The count() function returns the number of elements in the array.
    3. The last subscript of the array is count()-1.

    Foreach loop

    You can also use a "foreach" loop to take a little walk through the array.

    Example:

    Code:
    foreach ($names as $name) {
        echo 
    "$name<br />";

    In this example, PHP takes a little walk through each element of the array. Each element of the array gets assigned one at a time to the $name variable. Once the walk is over, the $name variable no longer exists.

    Associative Array

    An Associative array is just an array where you choose the keys. The previous arrays keys were integers.

    Example of declaring an associative array:

    Code:
    $people = array("james" => "15""john" => "30""sarah" => "20""bob" => "19"); 
    So in this situation the key "james" is mapped to his age "15" and the key "john" is mapped to his age "30".

    Returning Arrays

    In C++ you cannot explicitly return an array from a function. However in PHP you can. The syntax for return values from a function does not change.

    Example:

    Code:
    function getNames() {
        
    $names[] = "James";
        
    $names[] = "John";
        
    $names[] = "Chris";
        return 
    $names;

    Array Functions

    Merging Arrays


    By using the array_merge function you can merge 2 or more arrays together.

    Example:

    Code:
    $names = array("james""joe""john");
    $names2 = array("jill""judith");
    $names array_merge($names,$names2); 
    Unique arrays

    If you want an array that is unique, with no repeated items what would you do? You could sort it and a linear sweep comparing S[i] to S[i+1] and building a new array would work. But there is a built in function for this purpose:

    Code:
    $nums = array(1,1,2,3,5,8);
    $nums array_unique($nums); 
    The array would now look like (1,2,3,5,8);

    Is x in the array?

    You can use the in_array function to determine if an element is in the array.

    Given an array of the first 10 prime numbers, you want to determine if a random number is in the first 10 primes.

    Solution:

    Code:
    $primes = array(2,3,5,7,9,11,13,17,19,23);
    $rand rand(1,20);

    if (
    in_array($rand,$primes)) {
        echo 
    "$rand is in the first 10 primes.";
    } else {
        echo 
    "$rand is not in the first 10 primes.";

    Sorting

    PHP has quite a few functions for sorting. These functions are:
    • sort
    • asort
    • ksort

    The sort function doesn't try to keep the values of keys.
    The asort function sorts by the values in the array.
    The ksort function sorts by the keys.

    Example:

    Code:
    $a = array("1","3","2","4","-2","6");
    sort($a); 
    This array now looks like:

    Code:
    $a = array("-2","1","2","3","4","6"); 
    The keys in this situation have been reassigned. If you want to preserve the keys you want to use the ksort function.


    Example:

    Code:
    $names = array("james" => 15"sarah" => 20,"john" => 12);
    ksort($names); 
    This array now looks like:

    Code:
    $names = array("james" => 15"john" => 12"sarah" => 20); 
    Notice that the keys are in alphabetical order?

    The asort function would sort by the values. If the values are strings the keys will be in ascending order.

    Example:

    Code:
    $names = array("joe","bob","james","sarah","robin");
    asort($names); 
    This array now looks like:

    Code:
    $names = array("bob","james","joe","robin","sarah"); 
    If this array was a numeric array it would be in ascending order.

    Have a look at w3schools for a great reference of what you can do with arrays:
    PHP Array Functions

    Now try a few problems:

    The Trip
    Vanilla Primes - use an array to solve this problem.
    Stacks Of Blocks
    Greedy Gift Givers
    Deal Or No Deal scroll down to question 3.
    Tough Being A Teen scroll down to question 4.
    Now push yourself a little, with some sorting. Pinball Ranking watch the decimal places. Scroll to question 5.

    Again take a good look at the documentation for your programming language to see just what you can do with built-in code.

    Also if you decide to try the above problems make sure you read the specifications really closely. Remember it's all or nothing.

    Have fun,
    James
    Last edited by chili5; 03-18-2009 at 05:57 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: PHP Arrays

    See, if you keep doing this, you'll get popular and respected.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Jordan Guest

    Re: PHP Arrays

    Another fantastic tutorial! Very nice, +rep!

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: PHP Arrays

    Very nice. If you are interested in extending this, multi-dimensional arrays always throw people for a loop.

    Edit: I see you wrote one in Java. You are ahead of me.

  6. #5
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: PHP Arrays

    Great! Another good idea to write about.

  7. #6
    kemo's Avatar
    kemo is offline Newbie
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Re: PHP Arrays

    ty..

  8. #7
    Heagu is offline Newbie
    Join Date
    May 2009
    Posts
    11
    Rep Power
    0

    Re: PHP Arrays

    thanks for the guide!

  9. #8
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: PHP Arrays

    Your welcome.

+ 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