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];
echo $arrayname[1];
echo $arrayname[2];
?>
|
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'];
echo $second_array['key3'];
echo $first_array['key2'];
echo $second_array['key4'];
?>
|
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
$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
$flower_shop = array (
"rose" => "5.00",
"daisy" => "4.00",
"orchid" => "2.00",
);
echo "<table border='1' cellpadding='5'>";
echo"<tr><th>Flower</th><th>Price</th></tr>";
foreach($flower_shop as $Flower=>$Price)
{
echo "<tr><td>$Flower </td><td>$Price</td></tr> ";
}
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
$flower_shop = array(
"rose" => array( "5.00", "7 items", "red" ),
"daisy" => array( "4.00", "3 items", "blue" ),
"orchid" => array( "2.00", "1 item", "white" ),
);
echo "rose costs ".$flower_shop['rose'][0].", and you get ".$flower_shop['rose'][1].".";
echo "daisy costs ".$flower_shop['daisy'][0].", and you get ".$flower_shop['daisy'][1].".";
echo "orchid costs ".$flower_shop['orchid'][0].", and you get ".$flower_shop['orchild'][1].".";
?>
分享到:
相关推荐
主要介绍了PHP数组array类常见操作,结合实例形式分析了PHP数组array常见函数使用技巧与相关注意事项,需要的朋友可以参考下
给大家分享六个比较好用的php数组Array函数,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
array_change_key_case -- 返回字符串键名全为小写或大写的数组 array_chunk -- 将一个数组分割成多个 array_combine -- 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 array_count_values -- ...
下面小编就为大家带来一篇浅谈php数组array_change_key_case() 函数和array_chunk()函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
php二纬数组的array_unique
php数组合并 array_merge-php函数_.docx
PHP的扩展, 可以跟PHP的数组,因为PHP的数组是使用哈希表的, 所以内存使用比较大, 所以我用红黑树来写了一个dict的结构, 内存使用比PHP的数组少一半以上, 查询速度基本上跟PHP的数组相当, 不过插入速度稍微慢PHP的...
本文实例讲述了PHP实现数组array转换成xml的方法。分享给大家供大家参考,具体如下: <?php $elementLevel = 0 ; function array_Xml($array, $keys = '') { global $elementLevel; if(!is_array($array)) { if...
php数组合并 array_merge-php函数__2.docx
php数组合并 array_merge-php函数__1.docx
php数组1,这里是php技术中的数组部分的第一个png图像
PHP数组函数array_multisort()用法实例分析_.docx
php随机选择数组内容 array_rand-php函数_.docx
array_intersect() 定义和用法 array_intersect() 函数返回两个或多个数组的交集数组。 结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中的值,键名保留不变。 注释:仅有值用于比较。 语法 array...
“+”运算符和array_merge():array array_merge ( array array1, array array2 [, array ...] )都可以合并多个数组,但使用过程中有一点小区别。
php随机选择数组内容 array_rand-php函数__1.docx