Arrays (1)

Source: Internet
Author: User

First, the way the array is defined:

An array is a way to store data that can store multiple values, each of which is distinguished by a different key.

Number on box with key

Value-Box Contents

  

1 $arr Array (' name ' = ' Wangwu ', ' age ' = ');//define an array 2 Print_r ($arr); 3 Echo ' <br/> '; 4 Echo $arr [' Name '];//the value of the Access array key to name

Ii. Types of arrays

Classification of arrays: indexed arrays; associative arrays; two-dimensional arrays; multidimensional arrays

1, indexed array--the key does not have to have special meaning, the pure number from 0 increments the array, this is called "the Index array".

    

1 $arr Array (0=> ' A ',1=> ' B ',2=> ' C ',3=> ' d '); 2 Print_r ($arr);

2. Associative arrays

The string does the key, generally can reflect the content of the unit.

1 $arr = array (' name ' = ' Wangwu ', ' age ' + ', ' hobby ' = ' basketball ');//associative array 2 print_r ($arr); 

3, two-dimensional arrays, multidimensional arrays

$arr 2 = Array (' name ' = ' Lisi ', ' age ' =>25, ' hobby ' =>array (' Football ', ' Pingpang '));

Third, array key rules

1, the key can not be assigned, the system will 0,1,2 、、、 increment allocation

$arr = Array (' A ', ' B ', ' C ');

Print_r ($arr);

2, if there are key assignments, and some do not assign, will be from the previous key, take the largest key to start incrementing

    

1 $arr 1 Array (' name ' = ' Lisi ', ' age ' = ' ', ' running '); 2 Print_r ($arr 1);

Operation Result:

Array ([name] = Lisi [age] = [0] = running)

  

1 $arr 2 Array (0=> ' A ',1=> ' B ', ' C ',5=> ' d ', ' e ', ' f '); 2 Print_r ($arr 2);

Operation Result:

Array ([0] = a [1] = b [2] = = c [5] + d [6] = e [7] = f)

1 $arr 2 Array (0=> ' A ',1=> ' B ', ' C ',5=> ' d ', ' e ', ' f '); 2 Print_r ($arr 2);

Operation Result:

Array ([0] = a [1] = b [2] = = c [5] + d [6] = e [7] = f)

3. What if the key assignment repeats

Its keys are not repeatable for an array

If the key is repeated, the following key with the same name overrides the previous

  

1 $arr 3 Array (' A ', ' B ', ' C ',2=> ' d ',2=> ' e '); 2 Print_r ($arr 3);

Array ([0] = a [1] = b [2] = = e)

Attention:

There are only two types of keys for an array, integer and string

Keys are floating-point, string-type integers, and null

Key for floating-point and down-rounding

If the string is exactly understood as an integer, it is also converted to an integer

Null is understood by an empty string

    

1 $arr Array (2=> ' This ',2.5=> ' Day ',2.5=> ' is ', ' 2 ' = ' week ',null= ' end '); 2 3 Print_r ($arr);

Operating effect:

Array ([2] = = Week [] = end)

In the actual development:

Do not encounter these more troublesome arrays

Typically associative arrays and automatically generated indexed arrays

Iv. manipulating arrays of elements

The array is a composite data, which puts a lot of data print_r () will print out all the data in the array of specific units to operate arrays of elements, delete, change, check

1. Remove the value of a unit by itself and use the key to fetch the $ array name [key]

  

1 $arr Array (' A ', ' B ', ' C '); 2 Echo $arr [1];

Operation Result:

B

2. Take out the values of the two-dimensional array

The array hierarchy is at most 3 levels, otherwise it cannot be intuitively understood.

    

1 $arr 2 Array (' name ' = ' Lisi ', ' age ' =>23, ' hobby ' = =array(' Basketball ', ' Football ', ' Pingpang ')); 2 Echo $arr 2 [' Hobby '] [0];

Basketball

3. Change the value of an array cell

  

1 $arr 2 Array (' name ' = ' Lisi ', ' age ' =>23, ' hobby ' = =array(' Basketball ', ' Football ', ' Pingpang ')); 2 $arr 2 [' Hobby '] [0] = ' swiming '; 3 Print_r ($arr 2);

Operation Result:

Array ([name] = Lisi [age] = [hobby] = = Array ([0] = swiming [1] = = Football [2] = = Pingpang))

4. Add an array unit

  

1 $arr 2 Array (' name ' = ' Lisi ', ' age ' =>23, ' hobby ' = =array(' Basketball ', ' Football ', ' Pingpang ')); 2 $arr 2 [' area '] = ' Beijing '; 3 Print_r ($arr 2);

Operation Result:

Array ([name] = Lisi [age] = [hobby] = = Array ([0] = basketball [1] = = Football [2] = = Pingpang ) [Area] = Beijing)

5. Delete array cells

  

1 $arr 2 Array (' name ' = ' Lisi ', ' age ' =>23, ' hobby ' = =array(' Basketball ', ' Football ', ' Pingpang ')); 2 unset ($arr 2[' Hobby ']); 3 Print_r ($arr 2);

Operation Result:

Array ([name] = Lisi [age] = 23)

V. Iterating through an array

1. Remove each cell of the array

  

1 $arr Array (' A ', ' B ', ' C ', ' d '); 2 Echo $arr [0]. ' <br/> '; 3 Echo $arr [1]. ' <br/> '; 4 Echo $arr [2]. ' <br/> '; 5 Echo $arr [3]. ' <br/> ';

Operation Result:

A
B
C
D

1 $arr Array (' A ', ' B ', ' C ', ' d '); 2  for ($i= 0; $i<count($arr); $i+ +) {3     echo$arr{$i}, ' <br/> '  ; 4 }

2. How do associative arrays take out each array cell?

Its key is a string, there is no rule, foreach is specifically used to loop the array,

Very fast, the variable name ($k, $v) of the key value inside the foreach is any valid variable name

  

1 $arr Array (' name ' = ' Zhangsan ', ' age ' =>24, ' area ' = ' Beijing '); 2 foreach ($arras$k= +$v) {3     Echo $k, ': ',$v, ' <br/> '; 4 }

Name:zhangsan
Age:24
Area:beijing

3. Cyclic value only

  

1 $arr Array (' name ' = ' Zhangsan ', ' age ' =>24, ' area ' = ' Beijing '); 2 foreach ($arras$v) {3     Echo $v, ' <br/> '; 4 }

Operation Result:

Zhangsan
24
Beijing

4.array_keys-returns all the key names in the array foreach has no way to single loop out the health

  

1 $arr Array (' name ' = ' Zhangsan ', ' age ' =>24, ' area ' = ' Beijing '); 2 3 Print_r (array_keys($arr));

Operation Result:

Array ([0] + = name [1] = age [2] = area)

5. Change the value of each cell of the array below to twice times the original

  

1 $stu Array (' San ' =>3, ' Lisi ' =>4, ' Wang ' =>5, ' Zhao ' =>5); 2 foreach ($stuas$k= +$v) {3     $stu [$k$v* *; 4 }5print_r($stu);

Array ([san] = 6 [Lisi] + 8 [Wang] = [Zhao] 10)

Array (1)

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.