PHP Array (1) ____php

Source: Internet
Author: User
Tags numeric value scalar
1. Create an array
In programming languages, values and strings are called scalar variables, and a scalar variable can only represent a single numeric value or string. For example, $YDTX = the variable $ydtx in "mobile communication" is a scalar variable, and how do we use a variable to represent multiple values or strings in practice? For example, how to express "Communication base", "communication equipment", "Test Optimization", "3G", "LTE" and many other strings. In this case we need to use the array, although the array is complex but very useful, it can allow a variable to carry more information. The way to create an array is to use the array () function:
$YDTX = Array (
1 => ' Communication Basics ',
2 => ' communications equipment ',
3 => ' Test optimization ',
4 => ' 3G ',
5 => ' LTE '
);
An array must contain keys (indexes) and values, and the keys and values form an element, so the difference between an array and a scalar variable is that the array has multiple elements. Keys are composed of numeric values or strings, such as the above 1, 2, 3, 4, 5 is a number of keys (index), "Communication base", "communication equipment", "Test Optimization", "3G", "LTE" and so on are these keys corresponding values. Keys and values form an element of an array. The Print_r () function can output an array. To output the array above can be written in this way (<pre></pre> tags can be used to make the output structure clearer):
<pre>
<?php
$YDTX = Array (
1 => ' Communication Basics ',
2 => ' communications equipment ',
3 => ' Test optimization ',
4 => ' 3G ',
5 => ' LTE '
);
Print_r ($YDTX);
?>
</pre>

The output is shown in Figure 1:


Figure 1

2. Index array and associative array
An array of key values in an array is called an indexed array, and an array of key strings is called an associative array. Associative arrays are called hash lists in other programming languages. The array $ydtx mentioned earlier is the indexed array (because the keys are represented numerically):
$YDTX = Array (
1 => ' Communication Basics ',
2 => ' communications equipment ',
3 => ' Test optimization ',
4 => ' 3G ',
5 => ' LTE '
);
Sometimes in order to make sense of the values in an array, we need to represent the key of the value in a string, such as the key in the following code that describes the names of the chapters in the course "Communication basics" with a more meaningful string, which is an associative array:
$chapter = Array (
' Chapter One ' => ' Communication system Overview ',
' Chapter II ' => ' communication protocol ',
' The third chapter ' => ' Transmission technology ',
' Fourth chapter ' => ' modulation mode '
);
Here is the code to output the two types of arrays, this time we use the Var_dump () function to output the array, the Var_dump () function can output more detailed array information than the Print_r () function (the number of elements in the array can be displayed and the length of each string value):
<pre>
<?php
$YDTX = Array (
1 => ' Communication Basics ',
2 => ' communications equipment ',
3 => ' Test optimization ',
4 => ' 3G ',
5 => ' LTE '
);
Var_dump ($YDTX);
$chapter = Array (
' Chapter One ' => ' Communication system Overview ',
' Chapter II ' => ' communication protocol ',
' The third chapter ' => ' Transmission technology ',
' Fourth chapter ' => ' modulation mode '
);
Var_dump ($chapter);
?>
</pre>
The output of the above code is shown in Figure 2:

Figure 2

3, add elements to the array
Adding elements directly to an array needs to be very careful, to summarize the correct way to add:
(1) Adding elements to an indexed array
For example, an existing indexed array:
$YDTX = Array (
1 => ' Communication Basics ',
2 => ' communications equipment ',
3 => ' Test optimization ',
4 => ' 3G ',
5 => ' LTE '
);
To add an "indoor distribution system" to this array, the code is as follows:
$YDTX [6] = ' Indoor distribution system ';
Or
$YDTX [] = ' indoor distribution system ';
NOTE: If the keys are numeric, variable, or constant, then you do not need to quote them in quotes.
For an indexed array, if the value is not filled in in square brackets, the "Indoor distribution system" is added sequentially to the last, as shown in Figure 3:

Figure 3

(2) Adding elements to an associative array
For example, the following array:
$chapter = Array (
' The first chapter ' => ' The history of mobile communication
' Chapter II ' => ' Communication system Overview ',
' Chapter III ' => ' communication protocol ',
' Fourth chapter ' => ' Transmission technology
);
To add "modulation mode" to this array, the code is as follows:
$chapter [' fifth chapter '] = ' modulation mode ';
Unlike indexed arrays, you must specify a key when adding elements to an associative array, otherwise the added content will be strangely combined with the keys and values already in the group.
Summarize:
You must use the brackets whenever you add elements to an indexed array or to an associative array. Failure to raise an error without the brackets, such as using the following code, raises an error:
$YDTX = ' Indoor distribution system ';
The result of adding an array with this line of code is that the added value replaces the entire existing array, leaving only a string or numeric value.

Cond......

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.