Intro to Arrays in PHP

August 26, 2005

Php-1PHP has the ability to use associative arrays. These are by far the most useful and versatile data structure in PHP (and debatably any language). Instead of using the more common indexed array, where integers are used to identify the elements of the array, you can use strings.

If you are writing PHP, you have to know associative arrays and feel good about them.

Here's an example showing some common uses:

PHP:
  1. $arr = array('foo' => 'bar', 12 => true);
  2.  
  3. echo $arr['foo']; // bar
  4. echo $arr[12];    // 1
  5.  
  6. $arr['monkey'] = true;
  7.  
  8. echo $arr['monkey']; // true</code>

Associative arrays are also the most readable and convenient way to deal with database results. Suddenly you have an array that is indexed by field name from the table. How could anything be cooler than that?

For debugging arrays, there is nothing more useful than print_r():

PHP:
  1. $array = array(1, 2, 3, 4, 5);
  2. print_r($array);

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

If you want to add an element to the end of an array you can use the [] notation (sort of like a stack push):

PHP:
  1. $arr = array('Monkey', 'Alligator');
  2. $arr[] = 'Rabbit';
  3. print_r($arr);

Output:

Array
(
    [0] => Monkey
    [1] => Alligator
    [2] => Rabbit
)

Read More about PHP arrays on PHP.net.

array, arrays, associative, associative array, example, introduction, php, PHP.net, programming

Tags: , , , , , , , ,

Related:


Comments

4 Responses to “Intro to Arrays in PHP”

  1. NoSheep! » ADOdb - PHP Database Abstraction on August 27th, 2005 11:45 am

    [...] Comparing the two examples it’s clear how similar connecting to either database becomes. Also notice we’re setting the fetch mode to associative so we get back friendly associative arrays. The following is an example of how to query and get back results: [...]

  2. NoSheep! » CS3020 - PHP: Introduction on September 30th, 2005 2:13 pm

    [...] arrays [...]

  3. NoSheep! » Firefox Form Difficulty on November 4th, 2005 2:52 pm

    [...] So, I took the form and started some known array debugging techniques, mainly a simple print_r($_POST) which showed all the array elements with proper values in IE, but with Firefox they all had the default form values.  Strange. [...]

  4. rogotenin on July 6th, 2007 3:08 am

    Hi

    I am Lucy, I have found your website while searching for some info at Google. Your site has helped me in a big way.

    G’night

Got something to say?





User contributed tags: "PHP" push associative (145) - php array push (120) - php push associative array (60) - array_push associative (42) - php array_push associative (37) - php associative array push (36) - php push array (29) - php array_push associative array (28) - php array debug (27) - php array push associative (25) - array php (21) - php array_push_associative (20) - php debug array (19) - array push associative (16) - php add to array (15) - array_push associative array (15) - php array_push (15) - array push php (14) - php add item to front of array (14) - php add to front of array (13) - php5 array (13) - php array push front (12) - php associative array (12) - associative array php (11) - PHP associative arrays (11) - debug array php (11) - php "array in array" (10) - php array pushback (10) - push "associative array" php (10) - push associative array (10) - add to array php (10) - php array push back (9) - associative array push php (9) - array_push associative php (9) - php array fetch (8) - adodb fetch assoc (7) - array in array +php (7) - php array push associative array (7) - php5 array push (7) - array_push + associative arrays (7) - output array php (7) - all (7) - adodb fetch array (7) - php associative push (7) - array_push php associative (6) - push associative arrays (6) - array push associative +php (6) - push array php (6) - php arrays push (6) - array (6) - array_push_associative (6) - php associative array array_push (6) - php5 associative arrays (5) - php add to array push (5) - php array associative (5) - php array_push() + associative arrays (5) - php + push to associative array (5) - php adding to an array (5) - php associative array_push (5) - php push array associative (5) - debugging array php (5) - php 5 array (5) - php array output (5) - php associative arrays push (4) - array debugging php (4) - associative array_push (4) - php5 associative array (4) - print_r array (4) - t (4) - array push in php (4) - array_push_associative php (4) - add array php (4) - php array associative push (4) - array_push associative array php (4) - array php push (4) - php associative (3) - php associative array add push (3) - php clear associative array (3) - PHP add elements to associative array (3) - debug php array (3) - output associative array PHP (3) - debugging php arrays (3) - php5 push array (3) - php add associative array push (3) - php array add (3) - php4 array push (3) - php array mail (3) - php array push to front (3) - php arrays (3) - push associative php (3) - php associative array-of-arrays (3) - php array adding (3) - php adding values to an associative array (3) - php debug arrays (3) - debug arrays in php (3) - associative array php push (3) - php add element to front of array (3) - php "array in array" array_push (3) - Associative arrays in PHP4 (3) - associative array used add element in php (3) - php comparing associative arrays (3) - array push associative array php (2) - fetching elements from array php4 (2) - php adding to array (2) - array push php associative arrays (2) - PHP - Associative Arrays array_push (2) - add to array+php (2) - adodb + associative arrays (2) - add element front array php (2) - text arrays php (2) - php push array item (2) - php array_push associate (2) - array_push_associative in php (2) - adding terms to associative array php (2) - php add to associative array (2) - php debugging arrays (2) - php associative array add (2) - php associative push array (2) - php associate array push (2) - output an array php (2) - add to php array (2) - php push associated (2) - adding values to an associative array, php (2) - PHP adding to an associative array (2) - php associative array php.net (2) - array_push php associative array (2) - array add php (2) - php4 associative array (2) - php output associative array (2) - PHP array_push() for associative arrays (2) - add "array in array" php (2) - array_push($_POST (2) - how to debug a php array (2) - php push associative arrays (2) - pushing associative array in PHP (2) - $_POST values to an associative array (2) - php array add to front (2) - Assoc Array Push PHP (2) - php add associative array notice (2) - push ado results to a array in php (2) - pushing associative array php (2) - associative array of arrays in php (2) - php assoc array (2) - php add 2 arrays (2) - php array debugger (2) - clear an array in php (2) - output 1 array field + PHP (2) - php,array_push (2) - adding to an array +php (2) - php $array[1] (2) -