PHP Arrays (Cont.)


Associative Arrays (Cont.)
If the ID key of an associative array is a string, it could be doubly quoted or singly quoted.

 <html><body>
 <?php
   $hash["ID"] = 10;
   $hash['ID'] = 20;
   $hash[ID]   = 30;
   echo $hash["ID"] + $hash['ID'] + $hash[ID];
 ?>
 </body></html>
output =




According to the Stack Overflow, using array key names without quotes is a legacy feature in PHP. It was originally the way to do it, but it is no longer recommended and is only still supported for backward compatibility. This also shows the servers undcemcs01 (this page) and undcemcs02 use different versions of PHP, 7.2.34 and 5.4.16, respectively.

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. The arrays could be associative arrays too. It seems variable interpolation does not work for a multidimensional array element. For example,
   echo  "$families[$arr2][$i]<br />";
is not working. You have to use the following command instead:
   echo  $families[$arr2][$i] . "<br />";
 <html><body>
 <?php
  $size = 
  for ( $i=0; $i<$size; $i++ )
    for ( $j=0; $j<$size; $j++ )
      $arr[$i][$j] = ;

  $total = 0;
  for ( $i=0; $i<$size; $i++ )
    for ( $j=0; $j<$size; $j++ )
      $total += $arr[$i][$j];
  echo  $total;
 ?>
 </body></html>
Output =   
 <html><body>
 <?php
  $families = array (
    "Griffin" => array (
      "Peter",
      "Lois",
      "Megan"
    ),
    "Brown" => array (
      "Glenn",
      "Andrew",
      "Ella"
    )
  );
  echo  $families[][];
 ?>
 </body></html>
Output =