#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    232
    Rep Power
    1

    Question Why Associative Array Creation Has "=>" And Numerical Array Not ?


    Associative Array

    Code:
    <html>
       <body>
          
          <?php
             /* First method to associate create array. */
             $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);
             
             echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
             echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
             echo "Salary of zara is ".  $salaries['zara']. "<br />";
             
             /* Second method to create array. */
             $salaries['mohammad'] = "high";
             $salaries['qadir'] = "medium";
             $salaries['zara'] = "low";
             
             echo "Salary of mohammad is ". $salaries['mohammad'] . "<br />";
             echo "Salary of qadir is ".  $salaries['qadir']. "<br />";
             echo "Salary of zara is ".  $salaries['zara']. "<br />";
          ?>
       
       </body>
    </html>

    Numerical Array

    Code:
    <?php      
    /* First method to create array. */
    $numbers = array( one, two, three, four, five);
             
    foreach( $numbers as $value ) {
        echo "Value is $value <br />";
        }
             
        /* Second method to create array. */
        $numbers[0] = "one";
        $numbers[1] = "two";
        $numbers[2] = "three";
        $numbers[3] = "four";
        $numbers[4] = "five";
             
    foreach( $numbers as $value ) {
        echo "Value is $value <br />";
        }
    ?>
          
       </body>
    </html>
    Look at both code's bold parts on how each different type of arrays create an array.

    The Associative Array:
    /* First method to associate create array. */
    $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);


    The Numerical Array:
    /* First method to create array. */
    $numbers = array( 1, 2, 3, 4, 5);

    Why one uses "=>" and one not ? Are they not both procedural style ?
    Look:
    https://www.tutorialspoint.com/php/php_arrays.htm
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    Using => allows you to set keys. If you don't use it then PHP will assign numbers counting from 0.
    PHP Code:
    array(12345)
    array(
    => 1=> 2=> 3=> 4=> 5
    are the same thing.

    Comments on this post

    • UniqueIdeaMan agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    232
    Rep Power
    1
    Originally Posted by requinix
    Using => allows you to set keys. If you don't use it then PHP will assign numbers counting from 0.
    PHP Code:
    array(12345)
    array(
    => 1=> 2=> 3=> 4=> 5
    are the same thing.
    Well done with the answer.
    Thread will now be closed.

IMN logo majestic logo threadwatch logo seochat tools logo