百科狗-知识改变命运!
--

array() - 新建一个数组 - php 数组函数

是丫丫呀1年前 (2023-11-21)阅读数 21#技术干货
文章标签数组

array()

(PHP 4, PHP 5, PHP 7)

新建一个数组

说明

array([mixed $...] ): array

array() - 新建一个数组 - php 数组函数

创建一个数组。关于数组是什么的信息请阅读数组一节。

参数

语法“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

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)