php array defin...LOGIN

php array definition

Array Before, we let everyone remember two points:

1. Arrays can store multiple different types of data and are a composite data type.

2. The English name for array is array. Let’s learn the simplest array declaration.

Then let’s do a brief review:

<?php

$shu = array(1 , 1.5 , true ,'天王盖地虎,小鸡炖蘑菇');

echo '<pre>';
var_dump($shu);
echo '</pre>';

?>

In the above example, we found that we deposited:

1. Integer type

2 .Floating point

3.Boolean

4.String

Note: The main purpose of the echo pre tag in the above example is to output it as it is, and the format will be displayed better ,clearer.

We use var_dump() to print it out and see the effect:

QQ截图20161114115125.png

If we look carefully at the picture above, you will find the following features: :

1.array(size = 4) It means there are 4 elements in it

2.0 => int 1 We know that int means integer, and 1 is an integer value. So what do the previous 0, 1, 2, 3 and => mean?

3. The latest 0, 1, 2, and 3 represent the reading identification number of the value, which we call the subscript or key (English: key)

4.= > is a standard name for a symbol called: key-value correspondent. Therefore, later seeing 0=> int 1 can be said like this. The subscript accessor 0 corresponds to 1 of the integer type.

5. We also call the key-value pairs in the array elements, and elements are combinations of key-value pairs.

Oh yeah! Arrays seem to be quite easy to learn, as there are some rules.

Through the above example, you actually accidentally completed one of the declaration methods of an array: the declaration of an index array.

The so-called index array: it is an array whose subscripts are all integers.

Does the subscript of the index array have to start from 0?

Answer: This question is actually not true. The index array does not necessarily start from 0.

How can we not start from 0?

Answer: You need to use a small piece of knowledge you learned above. It’s the key-value correspondent. Let's start writing.

<?php

$kele = array('只有不断努力才能博得未来',10 => 'NoAlike', 'PHP中文网' , '去PHP中文网学PHP', 19 => '凤姐和芙蓉我都爱' , '杨幂我最爱');

//打印显示$kele
echo '<pre>';
var_dump($kele);
echo '</pre>';
?>

In the above example, we accidentally wrote an index array. However, the subscripts of the index array do not start from 0, but from 10.

However, in the above example, we feel that the writing is not beautiful. We can write the code more beautifully and the format is clearer.

<?php

$kele = array(
           '只有不断努力才能博得未来',
           10 => 'NoAlike',
           'PHP中文网' ,
           '去PHP中文网学PHP',
           19 => '凤姐和芙蓉我都爱' ,
           '杨幂我最爱'
       );


//打印显示$kele
echo '<pre>';
var_dump($kele);
echo '</pre>';
?>

Does this make it clearer? One row corresponds to the value of an array.

Let’s execute the code and see the effect:

QQ截图20161114115947.png

Through the above effect, let’s summarize the rules:

1. If the index array does not forcefully declare its subscript, its subscript starts from 0. (The value of our first array: Only through continuous efforts can we win the future. The subscript of this value is 0).

2. If I have specified a subscript, his subscript will be the value I specified. For example, the subscript 10 and the subscript 19 are the values ​​I have specified.

3. If a value (such as NoAlike) is forced to specify a subscript (the subscript is 10). The value added after it (PHP Chinese website), if the subscript is not specified. Their subscript growth pattern is maximum +1.

For example: I love both Sister Feng and Furong, the subscript is 19. I added at the end: Yang Mi is my favorite. Its subscript automatically increases to 11.

Unknowingly, you have learned how to create an index array, isn’t it amazing? So happy for you!

Add elements to the index array

After learning the creation of the index array, next we learn the adding and modifying the index array and delete.

<?php

$minren = array(
           '杨幂',
           '王珞丹',
           '刘亦菲',
           '黄圣依'
       );


//如何向这$minren这个数组中增加元素呢

//猜猜范冰冰的下标是多少?
$minren[] = '范冰冰';

$minren[100] = '范爷';

//它的下标又为几呢?
$minren[] = '李晨';

?>

Summary:

1. To add elements to the index array, use two methods: array variable name [], array variable name [key value] to add elements

2. The growth rules of key values ​​are the same as the previous rules. They are all based on the principle of adding 1 to the maximum value.

Delete elements from the index array

Let’s take the array just now as an example:

<?php

$minren = array(
           '杨幂',
           '王珞丹',
           '刘亦菲',
           '黄圣依',
           '范冰冰'
       );


//假设我不喜欢:黄圣依,如何将黄圣依给删掉掉呢?

//如果删除掉后范冰冰的下标为多少呢?

//如果在后面再追加一个元素,会填掉:“黄圣依”留下来的空吗?

unset($minren[3]);

$minren[] = '金星';


echo '<pre>';

var_dump($minren);

echo '</pre>';


?>

Look at the effect:

QQ截图20161114130646.png

1. Use unset to delete the variable to delete the value in the array.

2. Deleting the middle value will not automatically move the subsequent subscripts forward. But it is whatever the original value is.

3. Delete one of the values. The newly added value will not replace the original position, and still follow the principle of adding 1 to the maximum value.

Modify value

We learn the simplest creation, addition and deletion. I believe everyone will be able to reason out how to modify the value.

<?php

$minren = array(
           '杨幂',
           '王珞丹',
           '刘亦菲',
           '黄圣依',
           '范冰冰'
       );

$minren[5] = '范爷';

$minren[2] = '亦菲,不要嫁给韩国人好吗?';

echo '<pre>';

var_dump($minren);

echo '</pre>';


?>

Execute the above code and output it to see the result.

1. Use variable name [key] = new value. The values ​​in the array are modified.

Other ways to declare index arrays

Through the above example, we learned about the declaration of arrays. Let's learn other ways to declare arrays.

1. Directly use previously undeclared variables and declare the array by following the variable name with brackets.

<?php
    //直接写一个变量后面加上中括号,声明变量
    $qi[] = '可口可乐';
    $qi[10] ='百事可乐';
    echo '<pre>';
    var_dump($qi);
    echo '</pre>';
?>

2. It’s too troublesome to write using array() every time. You don’t need to write array, it’s simpler.

<?php

$minren = [
           '杨幂',
           '王珞丹',
           100 => '刘亦菲',
           '黄圣依',
           '范冰冰'
       ];

echo '<pre>';

var_dump($minren);

echo '</pre>';

?>

The above are two other ways of writing. Of course, you can use whichever one you like.

Associative array

The index array is appropriately transformed and an associative array appears. As long as there is an array of strings in the array, it is an associative array.

Through the above example, we found that the subscript of the array can only be a string, which of course cannot meet my needs.

Suppose I want to declare a subscript as handsome corresponding to the value of Eason Chan. It certainly doesn't meet my needs.

Then let’s declare the associative array. This is done in the same way as the declaration of the indexed array. But the difference is that the subscript of the string must be specified and the key-value correspondence must be used.

<?php

//声明一下关联数组
$rela = array(
       '帅' => '陈奕迅',
       '很帅' => '黄晓明',
       '灰常灰常帅' => '宁泽涛',
       '有男人味的大叔' => '吴秀波',
);




//再来玩玩简洁声明

$drink = [
        '美' => '凤姐',
        '很美' => '芙蓉姐姐',
        'verymei' => '杨幂',
        '心中滴女神呀' => '华妃',
        100 => '孙俪',
        '娘娘',
       ];


// 输出 $rela
echo '<pre>';

var_dump($rela);

echo '</pre>';


// 输出$drink

echo '<pre>';

var_dump($drink);

echo '</pre>';

?>

Let’s experiment to see what the final result is:

QQ截图20161114130928.png

We know through experiments:

1. Statement An associative array is a key name => value

2. An associative array can have elements of the index array

3. An element without a subscript is declared after the element of the index array in the associative array , it is still the maximum +1 principle. (Observe that the values ​​​​in the above picture are the two elements of Sun Li and Empress).

Adding, deleting, and modifying associative arrays

<?php 
$drink = [
             '美' => '凤姐',
             '很美' => '芙蓉姐姐',
             'verymei' => '王涛',
             '心中滴女神呀' => '杨澜',
             100 => '孙俪',
             '娘娘',
            ];
//追加方式与索引数组是一样的 
$drink['ynj'] = '伊能静'; 
//输出试试 
echo '<pre>';
var_dump($drink);
echo '</pre>'; 
//删除一个试试 
unset($drink['verymei']);
echo '<pre>';
var_dump($drink);
echo '</pre>'; 
//将芙蓉姐姐 改成:心里美才是真的美 
$drink['很美'] = '心里美才是真的美'; 
echo '<pre>';
var_dump($drink);
echo '</pre>'; 
?>

If you do the experiment just now, you will find out the operation methods and index operations Same way. It's just that the subscript is read a little differently.

Other ways of declaring associative arrays

<?php
    $drink['nf'] = '农夫山泉';
    $cocacola = '可口可乐';
    //当然可以是变量哟
    $drink['kl'] = $cocacola;
    $data = array(
        'kl' => $cocacola,
    );
    echo '<pre>';
    var_dump($drink);
    echo '</pre>'; 
?>

Through the above example, we found that in associative arrays, array can also be used directly behind the variable. Connect the brackets. Inserting the string subscript inside the brackets also declares success.

The example of inserting variables later just turns the string into a variable, of course there is no problem.

Inserting an array into an array

Arrays can be inserted into integers, floating point, and strings. So, can arrays be inserted into arrays?
sure.

When learning arrays, we defined such a group of nouns.

1. One-dimensional array There are no other arrays in the array, only some variables or values.

2. A single-layer array or multiple arrays are inserted into the two-dimensional array array

3. An array (B) is inserted into the three-dimensional array into the array (A). A one-level array (C) is inserted into the B array, which we call a three-dimensional array

4. Anything with more than three dimensions is called a multi-dimensional array.

Note: Other arrays can be inserted into both index arrays and associative arrays to make the arrays multi-dimensional.

Let's declare a one-dimensional array with only one dimension. Both indexing and association are possible. Everyone is familiar with this operation.

<?php

//一维的索引数组
$data = [1 , 2 , 3 , 4 ,  10 => 250];

//一维的关联数组

$rela = [
           'beijing' => '北京',
           'shanghai' => '上海',
           'tj' => '天津',
       ];

echo '<pre>';
var_dump($rela);
echo '</pre>';

echo '<pre>';
var_dump($data);
echo '</pre>';
?>

Everyone is familiar with one-dimensional arrays, and it is not difficult. Just declare the array. It's not difficult to make it two-dimensional, just insert one or more arrays into the array. If you learn two dimensions well, you will learn multidimensional ones well.

<?php

$person = array(

       'office' => '办公室',

       //注意:插入第一个数组哟
       'family' => array(

           '爸爸',
           '妈妈',
           'yeye' => '爷爷',
           'nn' => '奶奶',
       ),

       //注意:又插入了一个平级的数组
       'jiaotong' => array(
           '自行车',
           '摩托车',
           '汽车',
           '飞机',
       ),
);

echo '<pre>';
var_dump($person);
echo '</pre>';

?>

Let’s execute it and see the effect:

QQ截图20161114131215.png

We found that the variable person is an array type. There are three elements (size=3).

  • The first element is office, which corresponds to a string "office",

  • The second element is family, which is an array , this array has 4 elements, the values ​​are: dad, mom, grandpa, grandma.

  • The third element is jiaotong, which is also an array with 4 elements, and the values ​​are: bicycle, motorcycle, car, and airplane.

Okay, the two-dimensional statement is good. As long as the format is written correctly and you make sure you insert an array into an array.

How to access: What about the two values ​​​​of grandfather and father?

<?php
$person = [

       'office' => '办公室',

       //注意:插入第一个数组哟
       'family' => [

           '爸爸',
           '妈妈',
           'yeye' => '爷爷',
           'nn' => '奶奶',
       ],

       //注意:又插入了一个平级的数组
       'jiaotong' => [
           '自行车',
           '摩托车',
           '汽车',
           '飞机',
       ],
];

//访问“爸爸”这什值
echo $person['family'][0];

echo '<br />-----华丽丽的分割线------<br />';


//访问“爷爷”这什值
echo $person['family']['yeye'];

echo '<br />-----华丽丽的分割线------<br />';

//访问“汽车”这什值
echo $person['jiaotong'][2];


?>

Let’s look at the results as follows:

QQ截图20161114131256.png

In the above example, you will find that accessing the two-dimensional array is just to follow the previous subscript reading method. Just click below to read.

Write the variable first, write the subscript family in square brackets, and then write the subscript of the element to be accessed.

We have talked about the concept of three-dimensional array, so let’s start the experiment directly to see the effect:

<?php


$area = array(

       'china' => array(

           '上海',
           '湖北',
           '天津',
           '北京' => array(
               'hd' => '海淀',
               '朝阳',
               '房山',
               'cp' => '昌平',
           ),

           '广东' => array(
               '深圳',
               '广州',
               '佛山',
               'dg' => '东莞',

           ),

       ),


       'usa' => array(

           '华盛顿',
           '旧金山',
           '纽约' => array(
                   '曼哈顿区',
                   '皇后区',
                   '布鲁克林区',
           ),

       ),
);


echo '<pre>';
var_dump($area);
echo '</pre>';
?>

Let’s execute it and see the effect:

QQ截图20161114131432.png

Explanation:

There are two arrays under the variable $area, one for china and one for usa.

Shanghai, Hubei, Tianjin, and Beijing and Guangdong are inserted into the china array. And Beijing and Guangdong are another array. There are different elements in the two arrays of Beijing and Guangdong.

Insert Washington, San Francisco and New York in the USA into the array. And below New York is another array, describing the several districts below New York.

So, a three-dimensional array is to insert an array (A) into the array, and insert an array into the A array.

Let’s take a look at how to read the value inside.

<?php


$area = array(

       'china' => array(

           '上海',
           '湖北',
           '天津',
           '北京' => array(
               'hd' => '海淀',
               '朝阳',
               '房山',
               'cp' => '昌平',
           ),

           '广东' => array(
               '深圳',
               '广州',
               '佛山',
               'dg' => '东莞',

           ),

       ),


       'usa' => array(

           '华盛顿',
           '旧金山',
           '纽约' => array(
                   '曼哈顿区',
                   '皇后区',
                   '布鲁克林区',
           ),

       ),
);


//读取华盛顿
echo $area['usa']['0'];

//读取:布鲁克林
echo $area['usa']['纽约'][2];


//读取:昌平
echo $area['china']['北京']['cp'];

//修改cp下标的值改为:西城区

$area['china']['北京']['cp'] = '西城区';

//输出看看原来昌平的值是否发生了变化
echo $area['china']['北京']['cp'];

?>

Through the above we found that it is not difficult to learn arrays.


Difficulties in learning multi-dimensional arrays:

Pay attention to the format and tidy up the line breaks and indentations of each dimension. It’s not easy to make mistakes.

[Remember]
The separator between array elements is a comma. When inserting an array into an array, do not write a semicolon (;) at the end

3.png

Next Section
<?php $minren = array( '杨幂', '王珞丹', '刘亦菲', '黄圣依', '范冰冰' ); $minren[5] = '范爷'; $minren[2] = '亦菲,不要架给韩国人好吗?'; echo '<pre>'; var_dump($minren); echo '</pre>'; ?>
submitReset Code
ChapterCourseware