Reference arrays in string in PHP

Description

The following code shows how to reference arrays in string.

Example


<?php/*from  w  w w  .ja  v a  2 s.c om*/
  $monthInfo = array(
    1=>array("January", 31),
    array("February", 28),
    array("March", 31),
    array("April", 31),
    array("May", 31),
    array("June", 31),
    array("July", 31),
    array("August", 31),
    array("September", 30),
    array("October", 31),
    array("November", 30),
    array("December", 31));

  $userInfo = array(
    "Name"=>"Jack",
    "Location"=>"Main Street, California",
    "Occupation"=>"Programmer");

  //Curly braces alert the parser to
  //consider the entire array expression,
  //including the second dimension.
  print("{$monthInfo[1][0]} has {$monthInfo[1][1]} days<br>\n");

  //To avoid the confusion by keeping
  //the array values outside of the strings.
  print($monthInfo[1][0] . " has " . $monthInfo[1][1] . " days <br>\n");

?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone