Results 1 to 3 of 3

Thread: Array Functions in PHP

  1. #1
    Join Date
    May 2008
    Posts
    37

    Array Functions in PHP

    Array Functions in PHP

    array() - Creates an array
    array_change_key_case - Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
    PHP Code:
    array array_change_key_case  ( array $input  [, int $caseCASE_LOWER  ] ) 
    array_chunk() - Splits an array into chunks of arrays.
    array_combine() - Creates an array by using one array for keys and another for its values.
    array_count_values() - Returns an array using the values of the input array as keys and their frequency in input as values.

    Eg.

    PHP Code:
    <?php
    $array 
    = array(1"hello"1"world""hello");
    print_r(array_count_values($array));
    ?>
    Output :

    Array
    (
    [1] => 2
    [hello] => 2
    [world] => 1
    )
    array_diff() : Compares array values, and returns the differences

    array_diff_assoc() : Compares array keys and values, and returns the differences.

  2. #2
    Join Date
    May 2008
    Posts
    37

    Re: Array Functions in PHP

    array_filter : Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

    Examples

    PHP Code:
    <?php
    function odd($var)
    {
        return(
    $var 1);
    }

    function 
    even($var)
    {
        return(!(
    $var 1));
    }

    $array1 = array("a"=>1"b"=>2"c"=>3"d"=>4"e"=>5);
    $array2 = array(6789101112);

    echo 
    "Odd :\n";
    print_r(array_filter($array1"odd"));
    echo 
    "Even:\n";
    print_r(array_filter($array2"even"));
    ?>
    Output

    Odd :
    Array
    (
    [a] => 1
    [c] => 3
    [e] => 5
    )
    Even:
    Array
    (
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
    )

    array_diff_ukey() : Compares array keys, with an additional user-made function check, and returns the differences.

    array_merge() : Merges one or more arrays into one array.

    array_merge_recursive() : Merges one or more arrays into one array

    array_multisort() : Sorts multiple or multi-dimensional arrays

    array_pad() : Inserts a specified number of items, with a specified value, to an array.

    array_pop() : Deletes the last element of an array.

    array_product() : Calculates the product of the values in an array

  3. #3
    Join Date
    Jan 2009
    Posts
    16

    Re: Array Functions in PHP

    array_search — Searches the array for a given value and returns the corresponding key if successful.

    If needle is a string, the comparison is done in a case-sensitive manner.

    Examples

    PHP Code:
    <?php
    $array 
    = array(=> 'blue'=> 'red'=> 'green'=> 'red');

    $key array_search('green'$array); // $key = 2;
    $key array_search('red'$array);   // $key = 1;
    ?>

Similar Threads

  1. Components and functions of UML
    By NatureAddicted in forum Software Development
    Replies: 6
    Last Post: 22-02-2011, 03:47 PM
  2. What are the PHP libxml Functions?
    By taher in forum Software Development
    Replies: 4
    Last Post: 27-01-2010, 10:07 PM
  3. Assigning an array to an array
    By MACE in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 05:19 PM
  4. PHP fsockopen Functions
    By Japesh in forum Software Development
    Replies: 3
    Last Post: 22-05-2009, 06:02 PM
  5. Functions in PHP
    By Gyan Guru in forum Guides & Tutorials
    Replies: 3
    Last Post: 13-12-2008, 06:20 PM

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
  •  
Page generated in 1,751,673,920.57971 seconds with 16 queries