array() - 新建一个数组 - php 数组函数
array()
(PHP 4, PHP 5, PHP 7)
新建一个数组
说明
array([mixed $...] ): array创建一个数组。关于数组是什么的信息请阅读数组一节。
参数
语法“index => values”,用逗号分开,定义了索引和值。索引可以是字符串或数字。如果省略了索引,会自动产生从 0 开始的整数索引。如果索引是整数,则下一个产生的索引将是目前最大的整数索引+ 1。注意如果定义了两个完全一样的索引,则后面一个会覆盖前一个。
在最后一个定义的数组项目之后加一个逗号虽然不常见,却是合法的语法。
返回值
返回根据参数建立的数组。参数可以用=>运算符给出索引。关于数组是什么的信息请阅读数组一节。
范例
下面的例子演示了怎样建立一个二维数组,怎样给相应的数组指定键名,以及怎样在普通数组中略过和继续数字索引。
Example #1array()例子
Example #2array()的自动索引
以上例程会输出:
Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 13 [4] => 1 [8] => 1 [9] => 19 )
注意索引 3 被定义了两次,保留了最后的值 13。索引 4 在索引 8 之后定义,下一个自动生成的索引(值为 19 那个)为 9,因为最大的索引是 8。
本例建立了从 1 开始的数组。
从 1 开始索引的array()
以上例程会输出:
Array ( [1] => January [2] => February [3] => March )
在 Perl 中,可以访问在双引号内的数组的值。但在 PHP 中需要将数组用花括号括起来。
访问双引号内的数组
The above example will output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) 'short syntax associative array' The above example will output: Array ( [one] => 1 [two] => 2 [three] => 3 [four] => 4 )
If you need, for some reason, to create variable Multi-Dimensional Arrays, here's a quick function that will allow you to have any number of sub elements without knowing how many elements there will be ahead of time. Note that this will overwrite an existing array value of the same path. For example: Will display: Array ( [base] => Array ( [category] => Array ( [subcategory] => Array ( [item] => item_value ) ) ) )
The following function (similar to one above) will render an array as a series of HTML select options (i.e. "..."). The problem with the one before is that there was no way to handle , so this function solves that issue. function arrayToSelect($option, $selected = '', $optgroup = NULL) { $returnStatement = ''; if ($selected == '') { $returnStatement .= 'Select one...'; } if (isset($optgroup)) { foreach ($optgroup as $optgroupKey => $optgroupValue) { $returnStatement .= ''; foreach ($option[$optgroupKey] as $optionKey => $optionValue) { if ($optionKey == $selected) { $returnStatement .= '' . $optionValue . ''; } else { $returnStatement .= '' . $optionValue . ''; } } $returnStatement .= ''; } } else { foreach ($option as $key => $value) { if ($key == $selected) { $returnStatement .= '' . $value . ''; } else { $returnStatement .= '' . $value . ''; } } } return $returnStatement; } So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as an label. So, with this function, if only a single array is passed to the function (i.e. "arrayToSelect($stateList)") then it will simply spit out a bunch of "" elements. On the other hand, if two arrays are passed to it, the second array becomes a "key" for translating the first array. Here's a further example: $countryList = array( 'CA' => 'Canada', 'US' => 'United States'); $stateList['CA'] = array( 'AB' => 'Alberta', 'BC' => 'British Columbia', 'AB' => 'Alberta', 'BC' => 'British Columbia', 'MB' => 'Manitoba', 'NB' => 'New Brunswick', 'NL' => 'Newfoundland/Labrador', 'NS' => 'Nova Scotia', 'NT' => 'Northwest Territories', 'NU' => 'Nunavut', 'ON' => 'Ontario', 'PE' => 'Prince Edward Island', 'QC' => 'Quebec', 'SK' => 'Saskatchewan', 'YT' => 'Yukon'); $stateList['US'] = array( 'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'DC' => 'District of Columbia', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming'); ...
If you need to add an object as an array key, for example an object from Simple XML Parser, you can use the following. File.XML This This This The script: By telling the key to read the object as a string, it will let you set it. Hope this helps someone out!
"Quick and dirty" class to get an offset of multidimensional array by given path (sorry, that without comments).
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!