`
lzquan
  • 浏览: 197638 次
  • 性别: Icon_minigender_1
  • 来自: 东莞
社区版块
存档分类
最新评论

php 数组(array)

    博客分类:
  • PHP
阅读更多

Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language.

Arrays can most easily be described as an ordered list of elements. You can access the individual elements by referring to their index position within the array. The position is either specified numerically or by name. An array with a numeric index is commonly called an indexed array while one that has named positions is called an associative array. In PHP, all arrays are associative, but you can still use a numeric index to access them.

An Example of an indexed Array:

 

<?php
$seven = 7;
$arrayname = array( "this is an element", 5, $seven );

echo $arrayname[0];   //prints: this is an element
echo $arrayname[1];   //prints: 5
echo $arrayname[2];   //prints: 7
?>

As you can see, elements in an array can be any type of data (string, integer, double) and can also be other variables. An individual array element can be of any type, including another array.If you want to find out if a variable contains an array you can use the is_array() function. Notice that Indexed arrays start at position zero, not at position one, so your first array element has an index of 0, and the highest position in the array is one less than the number of elements in the array.

Associative Arrays

Associative arrays are arrays that use named keys that you assign to them. Have a look at the following example:

 

<?php
$first_array = array("key1" => "the first element", "key2" => "the second element");

$second_array = array(
    "key3" => "this is the first element of the second array",
    "key4" => "this is the second element of the second array",
);
echo $first_array['key1'];    //prints "the first element."
echo $second_array['key3'];   //prints "the first element of the second array"
echo $first_array['key2'];    //prints "the second element"
echo $second_array['key4'];   //prints "this is the second element of the second array"
?>

Right, now you know how to define an associative array, but you probably don't see yet how useful are they. Well think of this, say you have a flower-shop. You have 3 different flowers, and each flower has a different price. Let's make this example in php.

 

<?php
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array (
     "rose" => "5.00",
     "daisy" => "4.00",
     "orchid" => "2.00"
);

echo "rose costs $flower_shop['rose'], daisy costs $flower_shop['daisy'], and orchild costs $flower_shop['orchild'].";
?>

Because the indices in this associative array are not numbers, we cannot use a simple counter in a for loop to work with the array. We can use the foreach loop. In the following example we use the foreach loop to iterate through our flowers_shop array, and read them into a table. Note carefully the syntax.

 

<?php
//We initialize the array using the array() function. 
//Note that for readability one can spread the argument over several lines.

$flower_shop = array (
     "rose" => "5.00",
     "daisy" => "4.00",
     "orchid" => "2.00",
);
//let's print out the headers to our table
echo "<table border='1' cellpadding='5'>";
echo"<tr><th>Flower</th><th>Price</th></tr>";
//Now we start the foreach loop using the variable $flower to hold our key
//and $price to hold our cost.

foreach($flower_shop as $Flower=>$Price)
{
  echo "<tr><td>$Flower </td><td>$Price</td></tr> "; //print the values into a table cell for each iteration
}
//finally close the table
echo "</table>";
?> 

Multidimensional Arrays

In preceding example you've learned how to use arrays. But what if you want to give more information on each flower? You now have the cost, but what if you wanted to add the number of flowers you get for that price, and the colour of the flower? One of the ways to do it is using multidimensional arrays.

A multidimensional array is an array that contains at least one other array as the value of one of the indexes. Example below shows how to use multidimensional array:

<?php
//Initialize the array using the array() function.
$flower_shop = array(
"rose" => array( "5.00", "7 items", "red" ),
"daisy" => array( "4.00", "3 items", "blue" ),
"orchid" => array( "2.00", "1 item", "white" ),
);

//print "rose costs 5.00, and you get 7 items."
echo "rose costs ".$flower_shop['rose'][0].", and you get ".$flower_shop['rose'][1].".";
//print "daisy costs 4.00, and you get 3 items."
echo "daisy costs ".$flower_shop['daisy'][0].", and you get ".$flower_shop['daisy'][1].".";
//print "orchild costs 2.00, and you get 1 item.
echo "orchid costs ".$flower_shop['orchid'][0].", and you get ".$flower_shop['orchild'][1].".";
?>
评论

相关推荐

Global site tag (gtag.js) - Google Analytics