Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

9. Handling JSON Data: Client, Meet Server > A few (more) PHP rules...

A few (more) PHP rules...

There are a few more rules that will help us get the data we need, format it correctly, and get it onto our web pages.

Rules for arrays

  1. You can create new arrays using the array keyword, similar to JavaScript.

  2. You can access the array values using the index of the item, in square brackets [ ], like JavaScript. They are also zero-indexed, like JavaScript.

  3. Arrays can also be associative, which means you can use a key to access the item in the array, instead of the index. These are called key/value pairs.

  4. To assign a value to a key in an associative array, you use the => operator.

<?php
$my_arr2 = array('USA', 'China',
'Ireland');
echo $my_arr2[2]; // Prints "Ireland"

$arr = array("foo" => "bar", 12 => true);
echo $arr["foo"]; // Prints "bar"
echo $arr[12]; // Prints true
?>

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial