+ Reply to Thread
Results 1 to 8 of 8

Thread: PHP Arrays

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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 07:57 PM.

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,686
    Blog Entries
    57

    Re: PHP Arrays

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

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: PHP Arrays

    Another fantastic tutorial! Very nice, +rep!

  4. #4
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    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.

  5. #5
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: PHP Arrays

    Great! Another good idea to write about.

  6. #6
    Newbie kemo is an unknown quantity at this point kemo's Avatar
    Join Date
    Jun 2008
    Posts
    2

    Re: PHP Arrays

    ty..

  7. #7
    Newbie Heagu is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    11

    Re: PHP Arrays

    thanks for the guide!

  8. #8
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    Re: PHP Arrays

    Your welcome.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

+ 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. PHP 5 and OOP
    By Jordan in forum PHP Tutorials
    Replies: 11
    Last Post: 09-22-2008, 01:58 AM
  2. PHP 4 end of life announcement
    By Jordan in forum News
    Replies: 4
    Last Post: 08-30-2007, 09:55 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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