Useful PHP Functions To Work With Arrays

Discussion in 'PHP' started by pradeep, Nov 12, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    The in-built array functions allow you to interact with and manipulate arrays in various ways. Arrays are essential for storing, managing, and operating on sets of variables.

    Simple and multi-dimensional arrays are supported, and may be either user created or created by another function. There are specific database handling functions for populating arrays from database queries, and several functions return arrays.

    You can read more about working with PHP arrays here.

    In this article I'll demonstrate a few important and useful php functions, you can read and refer to the whole list of PHP functions on the official PHP website.

    array_push()



    Inserts one or more elements to the end of an array.

    PHP:
     $arr = array('Shabbir','Pradeep');
     
     
    //Add one more element
     
    array_push($arr,'Tanaz');
     
     
    print_r($arr);
     
     
    /*
     Output:
     Array
     (
         [0] => Shabbir
         [1] => Pradeep
         [2] => Tanaz
     )
     */
     

    array_pop()



    Deletes the last element of an array.

    PHP:
     $stack = array("orange""banana""apple""raspberry");
     
    $fruit array_pop($stack);
     
    print_r($stack);
     
     
    /*
     After this, $stack will have only 3 elements:
     Array
     (
         [0] => orange
         [1] => banana
         [2] => apple
     )
     
     and raspberry will be assigned to $fruit. 
     */
     

    array_shift()



    Removes the first element from an array, and returns the value of the removed element.

    PHP:
     $stack = array("orange""banana""apple""raspberry");
     
    $fruit array_shift($stack);
     
    print_r($stack);
     
     
    /*
     This would result in $stack having 3 elements left:
     Array
     (
         [0] => banana
         [1] => apple
         [2] => raspberry
     )
     
     and orange will be assigned to $fruit. 
     */
     

    array_unshift()



    Adds one or more elements to the beginning of an array.

    PHP:
     $queue = array("orange""banana");
     
    array_unshift($queue"apple""raspberry");
     
    print_r($queue);
     
     
     
    /*
     Output
     Array
     (
         [0] => apple
         [1] => raspberry
         [2] => orange
         [3] => banana
     ) 
     */
     

    array_unique()



    Removes duplicate values from an array.array_unique() takes input array and returns a new array without duplicate values.

    PHP:
     $input = array("a" => "green""red""b" => "green""blue""red");
     
    $result array_unique($input);
     
    print_r($result);
     
     
    /*
     Output
     Array
     (
         [a] => green
         [0] => red
         [1] => blue
     ) 
     */
     

    in_array()



    Checks if a value exists in an array.

    PHP:
     $os = array("Mac""NT""Irix""Linux");
     if (
    in_array("Irix"$os)) 
     {
         echo 
    "Got Irix";
     }
     if (
    in_array("mac"$os)) 
     {
         echo 
    "Got mac";
     }
     
     
    /*
     The second condition fails because in_array() is case-sensitive, so the program above will display:
     
     Got Irix
     */
     

    array_walk()



    Apply a user function to every member of an array.

    PHP:
     $fruits = array("d" => "lemon""a" => "orange""b" => "banana""c" => "apple");
     
     function 
    test_alter(&$item1$key$prefix)
     {
         
    $item1 "$prefix$item1";
     }
     
     function 
    test_print($item2$key)
     {
         echo 
    "$key$item2<br />\n";
     }
     
     echo 
    "Before ...:\n";
     
    array_walk($fruits'test_print');
     
     
    array_walk($fruits'test_alter''fruit');
     echo 
    "... and after:\n";
     
     
    array_walk($fruits'test_print');
     
     
    /*
     The above example will output:
     Before ...:
     d. lemon
     a. orange
     b. banana
     c. apple
     ... and after:
     d. fruit: lemon
     a. fruit: orange
     b. fruit: banana
     c. fruit: apple 
     */
     

    array_combine()



    Creates an array by using one array for keys and another for its values.

    PHP:
     $a = array('green''red''yellow');
     
    $b = array('avocado''apple''banana');
     
    $c array_combine($a$b);
     
     
    print_r($c);
     
     
    /*
     The above example will output:
     
     Array
     (
         [green]  => avocado
         [red]    => apple
         [yellow] => banana
     )
     */
     

    end()



    Sets the internal pointer of an array to its last element.This function can be used to get the last element of an array.

    PHP:
     $fruits = array('apple''banana''cranberry');
     echo 
    end($fruits); // cranberry
     
    These functions make our work a bit more easier, I consider these are the most frequently used array functions, but this is not all that PHP offers, a lot more can be found at the PHP website.
     
  2. trinitybrown

    trinitybrown New Member

    Joined:
    Oct 23, 2008
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    ALl scripts are very fine, do you have some idea about associative arrays and ksort() function as i tried to learn it but was unable to understand it , kindly help me on the topic
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    What exactly do you want to know?
     
  4. happyz

    happyz New Member

    Joined:
    Nov 18, 2008
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Its a nice post abt PHP array functions
    really like it
     
  5. haileyjonathan

    haileyjonathan New Member

    Joined:
    Mar 16, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi Pradeep,
    You wrote a wonderful scripts.It included PHP array function also. I appreciate to you..Thanks for your sharing also...
     
  6. LenoxFinlay

    LenoxFinlay Banned

    Joined:
    Apr 15, 2009
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.PHP internally stores all arrays as associative arrays, so the only difference between associative and indexed arrays is what the keys happen to be. Some array features are provided mainly for use with indexed arrays, because they assume that you have or want keys that are consecutive integers beginning at 0. In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer.PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. The order is normally that in which values were inserted into the array, but the sorting functions described later let you change the order to one based on keys, values, or anything else you choose.
     
  7. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    Like arrays in other languages, PHP arrays allow you to store multiple values in a single variable and operate on them as a set. PHP offers an extensive array manipulation toolkit—over 60 functions—that lets you process arrays in almost any way imaginable including reversing them, extracting subsets, comparing and sorting, recursively processing, and searching them for specific values.
    his document outlines some of the more useful functions in the PHP array with explanations and usage examples:
    Function:-
    sizeof($arr)
    array_values($arr)
    array_keys($arr)
    array_pop($arr)
    array_push($arr, $val)
    array_shift($arr)
    array_unshift($arr, $val)
    each($arr)
    sort($arr)
    array_flip($arr)
    array_reverse($arr)
    array_merge($arr)
    array_rand($arr)
    array_search($search, $arr)
    array_slice($arr, $offset, $length)
    array_unique($data)array_walk($arr, $func)
     
  8. lrvv

    lrvv New Member

    Joined:
    Sep 7, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.financealexa.com
    ok,thx for your sharing
     
  9. qforever

    qforever New Member

    Joined:
    Jan 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Here is much more detailed description of PHP array functions: php.net/manual/en/book.array.php
     
  10. qforever

    qforever New Member

    Joined:
    Jan 22, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Just to kill this topic:)
     
  11. loyo

    loyo New Member

    Joined:
    Feb 12, 2010
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://bbs.prog365.com
    good, thanks for your sharing.
    make a mark.
     
  12. loyo

    loyo New Member

    Joined:
    Feb 12, 2010
    Messages:
    36
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://bbs.prog365.com
    thank you for you share, keep you up.
     
  13. webdeveloperr

    webdeveloperr New Member

    Joined:
    May 18, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    its really nice and useful post..
    Thanks
     
    Last edited by a moderator: May 25, 2010
  14. ichandu

    ichandu New Member

    Joined:
    Oct 19, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    hi pradeep,

    This is good contribution from you.I really appreciate to you.Function is the strong part of any program.
    I am very much helpful to share a nice article.
    Thanks,

    ---------------------------------
     
  15. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    You can use $myArray[] = "values" instead of using array_push or array merge because they require more resources to use them(overhead of making a function call).
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice