Arrays (Array)

Source: Internet
Author: User
Tags http post

How to define an array:
    1. Using the array built-in keywords
    2. Use [] to define
    3. Direct Assignment
<?php
echo ‘<pre>‘;

//定义数组的方式
//
//1、使用array内置关键字
$arr = array(1,2,4);
print_r($arr);

//2、使用[]定义
$brr = [1,2,4];
print_r($brr);

//3、直接赋值
$crr[] = 1;
$crr[] = 2;
$crr[] = 4;

print_r($crr);

$drr[1] = 1;
$drr[2] = 2;
$drr[3] = 4;

print_r($drr);

Classification of arrays:Divide by dimension: one-dimensional arrays, two-dimensional arrays, multidimensional arrays are divided by subscripts: associative arrays, indexed arrays
<?php
echo ‘<pre>‘;
//数组的分类
//按照维度来分:一维、二维、多维
//一维数组
$arr = array(‘jack‘,‘boy‘,23);
print_r($arr);

//二维数组
$arr = array(array(‘jack‘,‘boy‘,23),array(‘rose‘,‘girl‘,18));
print_r($arr);

//多维数组
$arr = array(array(‘jack‘,‘boy‘,23,‘18nan‘=>array(18000,180,18)),array(‘rose‘,‘girl‘,18));
print_r($arr);
echo ‘//按照下标分
//下标是Int类型的 数组 叫索引数组
//下标是string类型的 数组 叫关联数组
//关联数组
$arr = array(‘username‘=>‘fengfeng‘,‘sex‘=>‘girl‘,‘age‘=>18,‘boyfriend‘=>array(‘xiaohei‘,‘xiaobai‘,‘xiaohuang‘));
print_r($arr);

//索引数组
$brr = array( 0=>‘jack‘,1=>‘boy‘,2=>23);
print_r($brr);

Note: In PHP, the subscript of an indexed array can be a non-sequential value, as long as the discontinuous subscript value is specified at initialization time. If the specified subscript value has already been declared, it is a value that is re-assigned to the variable. (the index value specified in front of it will be overwritten). If an element that does not specify an index value is mixed with an element that specifies an index value, the default index value for the element that does not specify an index value is incremented, immediately following the highest index value in the specified index value element.
<?php
echo ‘<pre>‘;
$count = array(1,14 =>‘gaomou‘,‘A gongsi‘,‘beijingshi‘,14=>‘0103243223‘,‘[email protected]‘);
print_r($count);
结果:
Array
(
    [0] => 1
    [14] => 0103243223
    [15] => A gongsi
    [16] => beijingshi
    [17] => [email protected]
)

How to access an array:Accessing a one-dimensional array is using the name and index values of the array, as well as the two-dimensional array. <?php
echo ‘<pre>‘;
$count = array(1,14 =>‘gaomou‘,‘A gongsi‘,‘beijingshi‘,14=>‘0103243223‘,‘[email protected]‘);

echo $count[14];

输出结果:0103243223

the analysis of direct assignment subscript:
<?php
echo ‘<pre>‘;
$arr[] = ‘a‘;
$arr[][] = ‘b‘;
$arr[1][] = ‘c‘;
$arr[1][][] = ‘d‘;
$arr[][] = ‘f‘;
$arr[][][] = ‘e‘;
print_r($arr);

Output Result: Array
(
[0] = a
[1] = = Array
(
[0] = b
[1] = C
[2] = = Array
(
[0] = d
)
)
[2] = = Array
(
[0] = f
)
[3] = = Array
(
[0] = = Array
(
[0] = = E
)
)
)

count () Gets the number of array cells or the number of object member properties
<?php
$arr = array(‘daoyan‘,‘zhuren‘,‘xiaozhang‘,‘jiaoshou‘,‘gaolaoshi‘);

//count()获取数组 单元个数 或者 对象成员属性个数
echo count($arr);
echo$num = count($arr);
echo $num;输出结果:5         5

There are several ways to iterate through an array:
1. Loop through (index) an array using the FOR statement:
<?php
$arr = array(‘daoyan‘,‘zhuren‘,‘xiaozhang‘,‘jiaoshou‘,‘gaolaoshi‘);

//方法一
$num = count($arr);
echo $num;
echo
//使用for循环,遍历连续的索引数组,关联数组会丢失无法打印出来
for($i = 0; $i < $num;$i++){
    echo $arr[$i].‘<br>‘;
}

//方法二
echo count($arr);

//不推荐使用,因为在for循环中count($arr)要执行N次,效率不高
for($i = 0; $i < count($arr);$i++){
    echo $arr[$i].‘<br>‘;
}


2. foreach () iterates through the array:Using a foreach statement to traverse an array is independent of the array's subscript, whether it is a contiguous array of numeric indices or an associative array with a string subscript, which can be traversed using a foreach statement.
The foreach statement has two syntax formats, the second of which is minor, but it is the first useful extension.
First syntax format: foreach ($arr as $value) {loop Body} The second syntax format: foreach ($arr as $key + $value) {loop body}
The first format iterates through the given array of $arr. In each loop, the value of the current element is assigned to the variable $value, and the pointer inside the value is moved backward one step, so the next iteration of the array will be given a second element of the arrays, until the end of the array stops looping, ending the traversal of the array.
The second format and the first format do the same thing, except that the key name of the current element is also assigned to the variable $key in each loop. <?php
$arr = [‘daoyan‘,‘user1‘ =>‘zhuren‘,‘username‘ =>‘xiaozhang‘,9=>‘jiaoshou‘,‘gaolaoshi‘];

//第一种,其中$v是任意值,随便取
foreach($arr as $v){
    echo $v.‘<br>‘;
}
echo ‘//第二种,将数组中每个单元的键和值全部输出,其中$k是键、$v是值,$k,$v同样是任意值,可随便取
foreach($arr as $k=>$v){
    echo ‘键:‘.$k.‘=>值‘.$v.‘}

Results: daoyan
zhuren
xiaozhang
jiaoshou
gaolaoshi
键:0=>值daoyan
键:user1=>值zhuren
键:username=>值xiaozhang
键:9=>值jiaoshou
键:10=>值gaolaoshi

3, combined use list (),Each () and while loop iterates through an array
Each () functioneach () function needs to pass an array as an argument, return the key/value pair of the current element in the array, and move the array pointer backwards to the next element's position. A key-value pair is returned as an array of four cells with the key name 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data. If the internal pointer crosses the end of the array, each () returns FALSE.
<?php
$brr = [‘lily‘,‘girl‘,‘age‘,‘xiaoming‘];

//each ---返回数组中当前的 键值对 并将数组指针向前移动一步

echo ‘<pre>‘;//1
$id=each($brr);
print_r($id);
//2
$bd=each($brr);
print_r($bd);
//3
$cd=each($brr);
print_r($cd);
//4
$fd=each($brr);
print_r($fd);
var_dump($fd);
//5
$qd=each($brr);
print_r($qd);
var_dump($qd);

Results: 1Array
(
    [1] => lily
    [value] => lily
    [0] => 0
    [key] => 0
)
2Array
(
    [1] => girl
    [value] => girl
    [0] => 1
    [key] => 1
)
3Array
(
    [1] => age
    [value] => age
    [0] => 2
    [key] => 2
)
4Array
(
    [1] => xiaoming
    [value] => xiaoming
    [0] => 3
    [key] => 3
)
4.array(4) {
  [1]=>
  string(8) "xiaoming"
  ["value"]=>
  string(8) "xiaoming"
  [0]=>
  int(3)
  ["key"]=>
  int(3)
}
5.bool(false)
  list () functionIt is not a real function, but rather a language structure for PHP.  List () assigns a set of variables in one step, assigning the values in the array to some variables. Note: List () can only be used for arrays of numeric indexes and assumes that the numeric index starts at 0.
Syntax format: list (mixed varname,mixed ...) =array_expression
The list () statement differs greatly from other functions in its use, and does not receive an array directly as a parameter. Instead, the value of each element in the array is assigned to each parameter in the list () function by assigning a value to the "=" operator. The list () function also converts each parameter in it to a variable that can be used directly in the script.
<?php
$brr = [‘lily‘,‘girl‘,‘age‘,‘xiaoming‘];

//list ---把数值中的值赋给一些变量
list($a,$b,$c) = $brr;

echo $a.‘<br>‘;
echo $b.‘<br>‘;
echo $c.‘<br>‘;

结果:   lily
        girl
        age

The while loop iterates through the array:
while (list ($key, $value) = each (array_expression)) {Loop Body}
<?php
echo ‘<pre>‘;
$arr =[‘user1‘=>‘zhanglaoshi‘,‘user2‘=>‘lilaoshi‘,‘user3‘=>‘wanglaoshi‘];

while(list($key,$value) = each($arr)){
    echo ‘key:‘.$key.‘=>value‘.$value.‘<br>‘;
}

结果:
key:user1=>valuezhanglaoshi
key:user2=>valuelilaoshi
key:user3=>valuewanglaoshi

Note: After each (), the array pointer stays in the next cell in the array, or when the end of the array is encountered, the last cell. If you want to iterate through the array again, you must use Reset ().
the difference between the two methods of the while and the foreach statement:Same point: The result of both traversing the array is the same. Different points: After iterating through the array with the while statement, the each () statement has pointed the incoming array parameter internal pointer to the end of the array. When you use the while statement to traverse the same array again, the array pointer is already at the end of the array, and the each () statement directly returns the False,while statement without executing the loop. The array pointer is re-directed to the first element only if the reset () function is called before the while statement executes. The foreach statement automatically resets the pointer position of the array, and when foreach starts executing, the pointer inside the array automatically points to the first cell. This means that you do not need to call the Reset function before the Foreach loop. Simply put: The while requires the reset () function to be called first, and foreach does not.
array pointer-related operation functions:Current (); key (); next ();p rev (); end (); Reset ();
<?php
//数组指针相关操作函数

echo ‘<pre>‘;
$arr = array(‘danyan‘,‘zhuren‘,‘xiaozhang‘,‘jiaoshou‘,‘gaolaoshi‘);
print_r($arr);

//current()返回指针指向的数据单元的值
var_dump((current($arr)));
var_dump(next($arr));
echo current($arr);

//返回指针的下一个位置所指向的数组单元的值
echo next($arr);
echo
//返回指针的上一个位置所指向的数组单元的值
echo prev($arr);
echo
echo end($arr);

//如果已经到了最后一个单元,再想访问下一个,会返回布尔值false
var_dump(next($arr));

//重置指针位置
reset($arr);

echo current($arr);

pre-defined array of hyper-global variables:
<?php
echo ‘<pre>‘;
// print_r($_SERVER);

//网站根目录
echo $_SERVER[‘DOCUMENT_ROOT‘];
echo ‘<br>‘;
// 客户端ip地址
echo $_SERVER[‘REMOTE_ADDR‘];
echo ‘<br>‘;

//来源地址
echo $_SERVER[‘HTTP_REFERER‘];

//当前执行的php脚本文件地址
echo $_SERVER[‘PHP_SELF‘];


// print_r($_ENV);
echo ‘<br>‘;


// 此关联数组包含 $_GET,$_POST 和 $_COOKIE 中的全部内容。
print_r($_REQUEST);


//通过 HTTP POST 方法传递的已上传文件项目组成的数组
$_FILES;

//通过 HTTP cookies 传递的变量组成的数组。是自动全局变量。
$_COOKIE;

//包含当前脚本中 session 变量的数组
$_SESSION;

$username = ‘alibaba‘;
print_r($GLOBALS);





















From for notes (Wiz)

Arrays (Array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.