PHP Basic Tutorial Eight array

Source: Internet
Author: User
Tags array definition php language

What this section explains

    • An Introduction to arrays

    • Creation of arrays

    • Dynamic growth of arrays

    • Traversal of an array

    • Correlation functions for arrays

    • Array operators

Objective

In the previous study, whether variables and constants, is a single data, and sometimes we have such a requirement, we need to put some of the same type of numerical values or some irrelevant values, reasonable storage, and use the time can be in accordance with some rules to extract data. At this time the preceding variables and so on can not meet the requirements, here is the use of arrays.

An Introduction to arrays

The array in PHP is different from the array of other languages, which is also caused by the loose nature of the PHP language syntax. What's different about that?

The so-called array, which is the same data type or different data type elements in a certain order of the set, that is, a finite number of variables named after a name, and then use the numbers to distinguish the set of their variables, this name is called the array name, the number is called subscript. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable.

The definition of an array is a popular understanding of the collection of data,

<?php    //Defines an array of data types that can be stored in any type    $arr = Array (12,true,false, ' ABCD ', 12.2);    Print_r ($arr);//output array. ..... Results...... Array ([0] = [1] = 1 [2] = [3] = ABCD [4] = 12.2)

You can see the various data types stored in the array, which can be output at the time of the output. And you can see that in the output, each value has a subscript in the corresponding, the subscript is called the key (key), and the value is values (value), we want to take a certain worth of time can be obtained by the key.

$a = array name [key name]echo $arr [0]; Results...... 12

When the key value of an array is a number, its subscript is calculated starting at 0.

So how did the array get created?

Three ways to create an array

    • There are three ways to define an array in PHP with an indexed array (the subscript is a number).

    • Associative array (subscript is a string)

    • The index of the array has both.

First Kind

<?php    //defines an array with values separated by commas.    $arr = Array (1,2,3,4, ' ABCD ', true);    Print_r ($arr);... Results...... Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = ABCD [5] = 1)

Use the array () language structure to create a new array, and from the output you can see that the subscript is a number. And the data is taken by the digital subscript. This is the indexed array.

The second Kind

<?php    //associative array, define the subscript yourself, use the = = to define the key and the value    $arr = Array (' a ' = = 1, ' b ' = = 2, ' c ' = ' = ' ABCDE ', ' d ' = ' + true ');    Print_r ($arr);... Results...... Array ([A] = 1 [b] = 2 [c] = ABCDE [d] = 1)

This method is also defined using the array () syntax structure, but the subscript of the array is defined by ourselves, and the subscript can be defined according to its own circumstances. About the key is worth to be aware of, Key will have the following cast

    1. A string containing a valid integer value is converted to an integral type. For example, the key name "8" will actually be stored as 8. However, "08" does not cast because it is not a valid decimal value.

    2. Floating-point numbers are also converted to integers, meaning that their fractional parts are removed. For example, key name 8.7 will actually be stored as 8.

    3. Boolean values are also converted to integral types. That is, the key name true is actually stored as 1 and the key name false is stored as 0.

    4. Null is converted to an empty string, that is, the key name null is actually stored as "".

    5. Arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.

If more than one cell in the array definition uses the same key name, only the last one is used, and the previous is overwritten.

The values in the associative array need to be taken using their own defined keys.

Third Kind

<?php    $arr [0] = 1;    $arr [2] = 2;    $arr [' a '] = N;    $arr [4] =;    Print_r ($arr); ..... Results...... Array ([0] = 1 [2] = 2 [A] = [4] = 45)

The definition of the third array can be defined directly in square brackets, which is assumed by default to be an array and created. As for subscripts, you can use numbers or strings. However, if more than one cell in the array definition uses the same key name, only the last one is used, and the previous is overwritten.

Dynamic growth of arrays

The length of the PHP array is not fixed, when you fill out the data, you can still fill in the data, and the length of the array will also increase, which is the dynamic growth of the PHP array.

<?php for    ($i = 0; $i < $i + +) {        $arr [] = $i;    }    Print_r ($arr); ..... Results....... Array ([0] = 0 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5 [6] = 6 [7] = 7 [8] = 8 [9] =& Gt 9)

The above code uses a for loop to assign values using the third definition of the array, but we can see that we do not write the subscript in the square brackets of the array, but the program does not give an error because the array is growing dynamically. If you do not specify a value for key, PHP will automatically use the maximum integer key name previously used plus 1 as the new key name. As a result, you can see that the subscript is automatically growing from 0 in the output.

Traversal of an array

Before we introduce the creation and basic use of arrays, how do we remove the values from all the arrays? In PHP, you can use the loop control process to take the values in the array, iterating through the array to get the values inside.

For

In the traversal of an array, you can use the for to values inside the array.

<?php    $arr = Array (1,9,4,2,8,10,5);    $sum = count ($arr);    for ($i = 0; $i < $sum; $i + +) {        echo $arr [$i]. '  ';    } ..... Results...... 1 9 4 2 8 10 5

The above count () is a system function that counts all the elements in an array, or something in an object. Thus to the size of the array, that is, the length. The $i is then used as the subscript for the array ($i starting from 0) through the For loop.

Foreach

In the above code, if our array is an associative array, that is, the subscript of an array is a string, then the above code is impossible to take out. So we can use another loop method provided by PHP: foreach (). There are two kinds of syntax:

foreach (array name as $key = = $value) {}foreach (array name as $value) {}

where "as" is a keyword and cannot be changed.

There can be three parameters in foreach, the first parameter is the name of the array, the second argument

Value is used to store values that are removed from the array. Of course the keys and worthy names can be taken on their own condition.

When the parameter inside is two, the $value is used only to store the value taken from the array, without the key.

<?php    $arr = Array (1,9,4,2,8,10,5);    foreach ($arr as $key = + $value) {        echo $key. ' = '. $value. '  ';    }    Echo ' <br> ';    foreach ($arr as $value) {        echo $value. '  ';    } ..... Results...... 0=>1 1=>9 2=>4 3=>2 4=>8 5=>10 6=>5 1 9 4 2 8 10 5

There is no output for the key in the second loop.

The two loop structures above are the methods we commonly use to iterate through arrays, and the second method, that is, a foreach method, is often used because this method works for all arrays, regardless of the subscript.

Correlation functions for arrays

  • COUNT ($array) counts all the elements in an array, or something in an object. Thus to the size of the array, that is, the length. Then pass the For Loop,

    I starting from 0).

  • Is_array ($arr) determines whether a variable is an array. Pass a variable in, or return TRUE if the variable is an array, or false;

  • Array_search (value) searches the array for the given value and returns the corresponding key name if successful. When we want to know if there is a number in an array, you can use this function, if the array has the value you want to find, then return the value corresponding to the key value.

  • Array_reverse ($arr), passing in an array, returns an array opposite the array.

    <?php$arr = Array (1,9,4,2,8,10,5), $array = Array_reverse ($arr);p rint_r ($array); Results...... Array ([0] = 5 [1] = [2] = 8 [3] = 2 [4] = 4 [5] = 9 [6] = 1)
  • Array_merge ($arr 1, $arr 2), pass in two arrays, and the values in an array are appended to the previous array. Returns an array as the result. If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.

  • Unset ($value) The variable can destroy an array, you can destroy a value in the array, when passed through the array [subscript] , it will destroy the value in the array, but when passed in the array, the entire array will be destroyed.

  • Sort () This function sorts the array. When this function ends, the array cell is rescheduled from lowest to highest. When the values in the array are numbers, you can use this function to sort them, but the result of the sort is that the numbers are in order from small to large.

    <?php    $arr = Array (1,9,4,2,8,10,5);    Sort ($arr);    Print_r ($arr);... Results..... Array ([0] = 1 [1] = 2 [2] = 4 [3] = 5 [4] = 8 [5] = 9 [6] = 10)
  • Usort ($array, Func_name) passes in an array, and a method name that is written by itself. Using a user-defined comparison function to sort the values in the array, you can only get from small to large in one of the above methods, but our requirements are from large to small, which will use this function, and the second parameter is the function of the collation we write.

    <?php$arr = Array (1,9,4,2,8,10,5), function mysort ($a, $b) {//write your own function to sort the array in order from large to small. if ($a = = $b) {    return 0;}    Return $a < $b? 1:-1;} Usort ($arr, ' Mysort ');p rint_r ($arr); Results...... Array ([0] = [1] = 9 [2] = 8 [3] = 5 [4] = 4 [5] = 2 [6] = 1)

    This is used to variable functions.

Array of Operators

We do not refer to array operators in the operator section, but arrays also have operators.

    • $a + $b Union of Joint $a and $b. The operator appends the array element on the right to the left array, and the key names in each of the two arrays are ignored on the right side of the array.

    • $a = = $b equal if the $a and $b have the same key/value pairs true.

    • $a = = = $b congruent if $a and $b have the same key/value pairs and the order and type are the same, TRUE.

    • $a! = $b is TRUE if the $a does not equal $b.

    • $a <> $b If the $a is not equal to $b true.

    • $a!== $b Not equal to TRUE if the $a is not all equals $b.
      In this case, the array operators are also well understood.

Summarize

Array here, is basically finished, the array in our development is often used, not only the above thought array, sometimes the array may also be an array, as long as the structure of the array in the mind has a clear understanding, no matter how many arrays, can be resolved, The presence of arrays also leads to some algorithms for arrays.

The above is the PHP Basic tutorial Eight of the contents of the array, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.