PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> PHP Arrays

PHP Arrays

FROM: AUTHOR: TIME:2008-03-29 HITS:

An array can store one or more values in a single variable name. phpsu


What is an array?

When working with PHP, sooner or later, you might want to create many similar variables. welcome to phpsu.com

Instead of having many similar variables, you can store the data as elements in an array.

phpsu is a phpschool

Each element in the array has its own ID so that it can be easily accessed. http://www.phpsu.com

There are three different kind of arrays: phpsu

  • Numeric array - An array with a numeric ID key
  • Associative array - An array where each ID key is associated with a value
  • Multidimensional array - An array containing one or more arrays

Numeric Arrays

A numeric array stores each element with a numeric ID key. phpsu.com is a free phpscool

There are different ways to create a numeric array. http://www.phpsu.com

Example 1

In this example the ID key is automatically assigned:

phpsu

$names = array("Peter","Quagmire","Joe"); 
do you kown phpsu.com?

Example 2

In this example we assign the ID key manually: phpsu is a phpschool

$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe"; www.phpsu.com

The ID keys can be used in a script:

phpsu.com

<?php 
phpsu.com
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe"; do you kown phpsu.com?
echo $names[1] . " and " . $names[2] . 
" are ". $names[0] . "'s neighbors";
?>
http://www.phpsu.com

The code above will output: welcome to phpsu.com

Quagmire and Joe are Peter's neighbors do you kown phpsu.com? 


Associative Arrays

An associative array, each ID key is associated with a value.

phpsu is a phpschool

When storing data about specific named values, a numerical array is not always the best way to do it. phpsu.com

With associative arrays we can use the values as keys and assign values to them. http://www.phpsu.com

Example 1

In this example we use an array to assign ages to the different persons: phpsu提供的php教程

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); welcome to phpsu.com 

Example 2

This example is the same as example 1, but shows a different way of creating the array:

do you kown phpsu.com?

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34"; do you kown phpsu.com?

The ID keys can be used in a script: phpsu

<?php 

http://www.phpsu.com

$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34"; phpsu.com is a free phpscool
echo "Peter is " . $ages['Peter'] . " years old.";
?> phpsu.com

The code above will output:

phpsu

Peter is 32 years old. 
phpsu.com


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.

welcome to phpsu.com

Example

In this example we create a multidimensional array, with automatically assigned ID keys: phpsu

$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
phpsu提供的php教程

The array above would look like this if written to the output: phpsu.com

Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
) phpsu.com

Example 2

Lets try displaying a single value from the array above: phpsu提供的php教程

echo "Is " . $families['Griffin'][2] . 
" a part of the Griffin family?"; phpsu.com is a free phpscool

The code above will output:

phpsu.com

Is Megan a part of the Griffin family? do you kown phpsu.com? 


TITLE:PHP Arrays
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Fri Jun 6 22:44:59 GMT-8 2008