php 数组(array)
Arrays can be used in many ways to store and organize data quicklyand efficiently. It is one of the more useful data types available toany programming language.Arrays can most easily be described as an ordered list of elements.You can access the individual elements by referring to their indexposition within the array. The position is either specified numericallyor by name. An array with a numeric index is commonly called an indexedarray while one that has named positions is called an associativearray. In PHP, all arrays are associative, but you can still use anumeric index to access them.
An Example of an indexed Array:
<?php
$seven = 7;
$arrayname = array( "this is an element", 5, $seven );
echo $arrayname; //prints: this is an element
echo $arrayname; //prints: 5
echo $arrayname; //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. Anindividual array element can be of any type, including another array.Ifyou want to find out if a variable contains an array you can use theis_array() function. Notice that Indexed arrays start at position zero,not at position one, so your first array element has an index of 0, andthe highest position in the array is one less than the number ofelements 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 youprobably don't see yet how useful are they. Well think of this, say youhave a flower-shop. You have 3 different flowers, and each flower has adifferent 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, wecannot use a simple counter in a for loop to work with the array. Wecan use the foreach loop. In the following example we use the foreachloop to iterate through our flowers_shop array, and read them into atable. 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 ifyou want to give more information on each flower? You now have thecost, but what if you wanted to add the number of flowers you get forthat price, and the colour of the flower? One of the ways to do it isusing multidimensional arrays.
A multidimensional array is an array that contains at least oneother array as the value of one of the indexes. Example below shows howto 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'].", and you get ".$flower_shop['rose'].".";
//print "daisy costs 4.00, and you get 3 items."
echo "daisy costs ".$flower_shop['daisy'].", and you get ".$flower_shop['daisy'].".";
//print "orchild costs 2.00, and you get 1 item.
echo "orchid costs ".$flower_shop['orchid'].", and you get ".$flower_shop['orchild'].".";
?>
页:
[1]