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

Associative Array

<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


<?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:

The difference is not between numerical or associative arrays, it’s just the method you use to create the array.
You could create an associative array without => if you wanted.

$salaries['mohammad'] = 2000;
$salaries['qadir'] = 1000;
$salaries['zara'] = 500;

foreach($salaries as $name => $value) {
 echo "Salary for $name is $value <br>\n" ;
}

In PHP there is usually more than one way to do something, like make an array. The method you choose often depends on circumstances.
Using the array() function, you will use => only if you want it to be associative, otherwise default numeric key are applied.
You would generally use the array() function to set many values in one go.

But the $arrayName[] = $value syntax would generally be used to add just one value to an array at a time, and the syntax is the same as setting a variable, using $variableName = $value
So you may use that method when you don’t have all of the values at the time you begin to create the array. It is used to add to, append or edit values in an array.

    $numbers[0] = "one";
    $numbers[1] = "two";
    $numbers[2] = "three";
    $numbers[3] = "four";
    $numbers[4] = "five";

Here you could use:-

    $numbers[] = "one";
    $numbers[] = "two";
    $numbers[] = "three";
    $numbers[] = "four";
    $numbers[] = "five";

As the default numeric keys will be added automatically. You only need state them if it’s an associative array, or you want a different order, or are editing a value to something else.

3 Likes

Reference back to what I have said here.

https://sitepoint.com/community/t/how-come-variable-not-assigned-and-php-knows-where-to-look-for-value/262775/5?u=spaceshiptrooper

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.