Home arrow PHP arrow Page 4 - PHP: The Switch Statement and Arrays

Associative Arrays - PHP

In our last exciting adventure (back in early November), we braved crocodiles, ravenous editors, most of the PHP statements, and beginning loops. In this edition we'll cover the final statement, the Switch, and discuss arrays. So sit back, order your R2D2 robot to bring you a cold, frosty Jolt Cola, and let's get cracking.

TABLE OF CONTENTS:
  1. PHP: The Switch Statement and Arrays
  2. The Variable's Big Brother
  3. Printing the Array Values
  4. Associative Arrays
  5. The Multidimensional Array
  6. Beyond Multidimensional Arrays
By: James Payne
Rating: starstarstarstarstar / 6
January 07, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Associative Arrays use an ID key that is not necessarily a numeric value. Here is an example:


$mystats = array(“Weight”=>900, “Looks”=>1, “IQ”=>12);

Another way to create an associative array is like this:


$mystats['Weight'] = 900;

$mystats['Looks'] = 1;

$mystats['IQ'] = 12;

If it still isn't clear, this following coding example should make it so:


<html>

<body>

<?php


$mystats['Weight'] = 900;

$mystats['Looks'] = 1;

$mystats['IQ'] = 12;


echo “I weigh “ . $mystats['Weight'] . “pounds.”;

}

?>

The above code would result in the following:

  I weigh 900 pounds.

As you can see, we call on the index (in this instance "Weight") to retrieve how much I weigh, which prints the value 900. If I had called on "Looks" it would have printed 1, and so forth. So instead of referencing the values in the array with numbers, we just did it with word labels to make it easier to remember and understand (you wouldn't label your files in a file cabinet with numbers, unless you were The Count from Sesame Street that is).



 
 
>>> More PHP Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: