Working With Arrays in PHP

Discussion in 'PHP' started by Sanskruti, Apr 14, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    Arrays are common to many programming languages. They are special variables which can hold more than one value, each stored in its own numbered 'space' in the array. Instead of having many similar variables, you can store the data as elements in an array.Each element in the array has its own ID so that it can be easily accessed.

    There are three different kind of arrays:
    • Numeric array - An array with a numeric ID key
    • Associative array - An array where each ID key is associated with a value
    • Multidimensional array - An array containing one or more arrays

    Numeric Arrays



    A numeric array stores each element with a numeric ID key. There are different ways to create a numeric array.

    Example 1

    In this example the ID key is automatically assigned:

    Code:
    $names = array("John","Paul","Steve");
    Example 2

    In this example we assign the ID key manually:

    Code:
    $names[0] = "John";
    $names[1] = "Paul";
    $names[2] = "Steve";
    The ID keys can be used in a script in the following way

    Code:
    <?php
    $names[0] = "John";
    $names[1] = "Paul";
    $names[2] = "Steve";
    echo $names[1] . " and " . $names[2] . 
    " are ". $names[0] . "'s Friends";
    ?>
    The code above will output:

    Paul and Steve are John's Friends

    Associative Arrays



    An associative array, each ID key is associated with a value.When storing data about specific named values, a numerical array is not always the best way to do it.With associative arrays we can use the values as keys and assign values to them.

    Example 1

    In this example we use an array to assign ages to the different persons:

    $ages = array("John"=>32, "Paul"=>30, "Steve"=>34);

    Example 2

    This example is the same as example 1, but shows a different way of creating the array:

    Code:
    $ages['John'] = "32";
    $ages['Paul'] = "30";
    $ages['Steve'] = "34";
    The ID keys can be used in a script:

    Code:
    <?php
    $ages['John'] = "32";
    $ages['Paul'] = "30";
    $ages['Steve'] = "34";
    echo "John is " . $ages['John'] . " years old.";
    ?>
    The code above will output:

    John is 32 years old.

    Multidimensional Arrays



    In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

    Example

    In this example we create a multidimensional array, with automatically assigned ID keys:

    Code:
    $families = array
    (
      "Griffin"=>array
      (
      "John",
      "Lois",
      "Megan"
      ),
      "Paul"=>array
      (
      "Glenn"
      ),
      "Brown"=>array
      (
      "Cleveland",
      "Loretta",
      "Junior"
      )
    );
    The array above would look like this if written to the output when done through print_r function:

    Code:
    Array
    (
    [Griffin] => Array
      (
      [0] => John
      [1] => Lois
      [2] => Megan
      )
    [Paul] => Array
      (
      [0] => Glenn
      )
    [Brown] => Array
      (
      [0] => Cleveland
      [1] => Loretta
      [2] => Junior
      )
    )
     
  2. 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
    You can initialize Numeric Arrays like this also,

    PHP:
    $names = array();
    $names[] = 'Pradeep';
    $names[] = 'Shabbir';
    $names[] = 'Manindar Poddar';
    // PHP will automatically set the index
    You can print associative arrays like this,

    PHP:
    $ages['John'] = "32";
    $ages['Paul'] = "30";
    $ages['Steve'] = "34";

    //using sprintf or printf
    printf("John is %d years old",$ages['John']);

    // or this way
    print("John is {$ages['John']} years old");
     
    Scripting likes this.
  3. shrikrishnatech

    shrikrishnatech New Member

    Joined:
    Nov 19, 2010
    Messages:
    42
    Likes Received:
    4
    Trophy Points:
    0
    Occupation:
    Wordpress theme, Wordpress Theming, WP Themes, Cor
    Home Page:
    http://www.shrikrishnatechnologies.com
    thanks for this code
     
  4. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Amazing ...
     
  5. Bradpitt

    Bradpitt New Member

    Joined:
    Aug 27, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Assuming that you have three arrays which might contain different values as follows.:

    PHP:
    $arr1 = array('1''5''10');
    $arr2 = array('1''3''10');
    $arr3 = array('1''6''10');
    How would you strip out what's different and get it as follows?:

    $arr1 = array('
    1', '10');
    $arr2 = array('
    1', '10');
    $arr3 = array('
    1', '10');
    I meant I wanted to get it as follows.:

    $result = array('
    1', '10');
    ______________________
     
  6. mukeshsoftona

    mukeshsoftona Banned

    Joined:
    Oct 28, 2011
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    <?php
    function array_get_key_val($key, $heystack) {
      if (is_array($heystack)) {
        foreach ($heystack as $k => $v) {
          if ($k == $key) {
            return $v;
          }
          elseif (is_array($v)) {
            return array_get_key_val($key, $v);
          }
        }
      }
      return FALSE;
    }
    ?>
     
    Last edited by a moderator: Nov 14, 2011
  7. Scallar

    Scallar New Member

    Joined:
    Nov 15, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    <?php
    function arrays($array1,$array2)
    {
    echo $array1.' '.$array2;
    }
    arrays(myname,lastname);
    echo arays();
    ?>
    Is this code is correct and where is the fault and how i can change it . i want that user enter value in it and it show its name and last name in an array in the database.
     
  8. c_user

    c_user New Member

    Joined:
    Aug 23, 2009
    Messages:
    86
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    Php dev
    Location:
    Bhubaneswar
    If u want that the user should enter the values and it should be displayed on the database. Then you have to accept the values from the form and use the command : mysql_query("INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)"); to insert into the database
     
  9. designfirm

    designfirm New Member

    Joined:
    Jan 5, 2012
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Nice post.......
     
  10. donor

    donor Banned

    Joined:
    Jan 12, 2012
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    web developer
    Location:
    india
    <?php
    $arr = array("foo" => "bar", 12 => true);

    echo $arr["foo"]; // bar
    echo $arr[12]; // 1
    ?>
     
  11. newskeybd

    newskeybd New Member

    Joined:
    Jan 23, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.newskeybd.com
    Thanks Four Your Array Information
     
  12. packageshipping

    packageshipping New Member

    Joined:
    Jun 13, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    USA
    Home Page:
    http://www.shipsus.com/
    There are three type of array in PHP, Numeric Array, Associative Array and Numeric Associative array. Thanks for defining three types of array in well manner.
     
  13. The Alchemist

    The Alchemist New Member

    Joined:
    Jul 7, 2012
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Is it possible to store more than one kind of value in an array in PHP, say a string and a integer?
     
  14. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    As far as I know it's not possible, but I may be wrong :3
     
  15. shomameera

    shomameera New Member

    Joined:
    Aug 24, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    this information is very useful for the php developers ... thank u so much for the information
     
  16. cyclop

    cyclop New Member

    Joined:
    Nov 20, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for this quick tip.
     

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