Why Associative Array Creation Has "=>" And Numerical Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Why Associative Array Creation Has "=>" And Numerical Array

Post by UniqueIdeaMan »

Associative Array

Code: Select all

<html>
   <body>
      
      <?php
         /* First method to associate create array. */
[b]         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);[/b]
         
         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: Select all


<?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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why Associative Array Creation Has "=>" And Numerical Ar

Post by Celauran »

I already answered this yesterday. viewtopic.php?f=1&t=143645#p708466
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Why Associative Array Creation Has "=>" And Numerical Ar

Post by Christopher »

You understand that the autoincrementing keys starting at zero is the shortcut syntax, and that you can specify keys for a numerical array to if you want to do it in long hand notation:

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