PHP array definition and variable destruction and one-dimensional array sorting _php tutorial

Source: Internet
Author: User
Tags array definition
This article first introduces the definition of the array in PHP and the destruction of the array, and then use an example to illustrate the sorting method of one-dimensional data, the need for friends can refer to, I hope to be useful to everyone.

Definition of PHP array

The code is as follows Copy Code

......
$array = Array (' One ', ' one ', ' one ', ' three ');
Var_dump ($array);
......

In the first line of this code fragment, we define a one-dimensional array $array, and in the second row we format the output of this array, and the result is this:

The code is as follows Copy Code

Array (3) {
[0]=>
String (3) "One"
[1]=>
String (3) "both"
[2]=>
String (5) "three"
}

Now the brother explains this output, first the first line of the output of this array,
Array (3) tells us that this is an array of three elements,
First, the No. 0 element is a string with a length of 3 (String (3)) ...
The length of the relationship is two left to say, roar ... what does this mean? This means that in PHP if we do not specify the subscript for the array, then he will be his own self-initiated from scratch to get us a subscript-that is, the key name in the PHP array; and then look at the following example:

The code is as follows Copy Code
......
$array = Array (' One ', "hello" = = ' ", ' three ');
Var_dump ($array);

...... This time when we define the array, we specify the subscript for the second element (since then, it is called the key name in this article, the subscript is a bit vague!). ), the second element specifies the key name (hello), let's look at the output:

The code is as follows Copy Code

Array (3) {
[0]=>
String (3) "One"
["Hello"]=>
String (3) "both"
[1]=>
String (5) "three"
}

I think you also like me to see a very intelligent phenomenon, the first element of the key name or 0--this we can understand, because we do not specify, and the PHP array must have a key name, PHP has to start from scratch to create a key name, the second element when we specify the key name, PHP uses this key name in respect of our opinions; complex things in the third element,

The third element, which looks very simple, we do not specify the key name, PHP automatically put the maximum integer key name plus 1, as the key name. But have you thought about it, if we change the key name of the first element to "-5", the second element has the same key name, what will the result be? We wait and see:

The code is as follows Copy Code
......
$array = Array ( -5=> ' one ', "hello" = = ' ", ' three ');
Var_dump ($array);
......

If you take it for granted that the third element's key name should be-4, then I'll tell you that the idea was right before PHP4.3.0, but then it was wrong, and now that's the version after PHP4.3.0, you'll see the following result:

The code is as follows Copy Code

Array (3) {
[-5]=>
String (3) "One"
["Hello"]=>
String (3) "both"
[0]=>
String (5) "three"
}

Yes, the third element is starting from 0, that is, no matter how small your negative number, the next element if you let PHP to define the key name, then he is starting from 0-remember this place is that in the existing key name if the largest is still a negative number, no matter how small negative numbers, PHP also from zero to start the next key name.

The destruction of the PHP array is simple, like destroying other variables.
Destroy entire array: unset ($array)
Destroys an element in an array: unset ($array [-5])

In real life and in the world of procedure, order is always important--I can't imagine what a world without order would look like! PHP arrays are no exception. PHP provides four sets of functions for the ordering of PHP arrays, the first three are for one-dimensional arrays, we first say that the three, the fourth kind of multidimensional array of the sort in the next article, that's a bit complicated.

The first group: sort and Rsort, sorted by the order of the PHP array key values ASC and reverse desc, while destroying the index relationship of the original array-in fact, after dropping the index, re-establishes the 0-based numeric index. Take a look at the routines:


The code is as follows Copy Code

$a = Array ("a" =>1,2);
Sort ($a);
Var_dump ($a);

Rsort ($a);
Var_dump ($a);
?>

Take a look at the first output, the first output:
Array (2) {
[0]=>
Int (1)
[1]=>
Int (2)
}
A second output:
Array (2) {
[0]=>
Int (5)
[1]=>
Int (4)
}


Where did you find the index a we originally defined? Where did it go? To be sure, they have been ruthlessly deleted, if you do not care about the original index relationship, you can use them!

The second set of functions: Asort and Arsort, the two functions are a bit more powerful, as long as they can preserve the original index of the array, the above example of the sort and rsort with the two functions, respectively, to see the results of the operation:

The code is as follows Copy Code
Array (2) {
["A"]=>
Int (1)
[0]=>
Int (2)
}
Array (2) {
[0]=>
Int (2)
["A"]=>
Int (1)
}

This one can understand, needless to say!

The third set of PHP array sorting functions: Krsort and Ksort These two are different from the above two groups, these two functions are the key name of the order, we can replace the above example of the function of these two, to see the specific operation results, here also do not say, otherwise this article is too long to write, Afraid that some brothers have no patience to see the focus of this article, although the focus is below!

Using a custom function to sort the PHP array, there are three functions:
Uasort uses a custom function to sort the key values of the PHP array and preserves the original index relationship.
Uksort uses a custom function to sort the key names of the PHP array and preserves the original index relationship.
Usort sorts the key values of the PHP array by using a custom function, and deletes the original index relationship, creating a new index from scratch.

This place certainly needs an example:


Output Result:
Array (4) {
[0]=>
Int (1)
[3]=>
Int (5)
[1]=>
Int (4)
[2]=>
Int (3)
}

Oh...... Is it more chaotic than not sorting? We are only demonstrating the use of the method, the specific you use the time to do it yourself! If you do not manipulate these values then compare them, for example we are here:
$a +=1;
$b +=3;//to compare these values after changing them


In the PHP manual, a total of 74 array functions are defined, covering the definition of the PHP array, assignment, sorting, numeric manipulation, comparison, and the key name of the array, key value reversal ... And so on we can almost think of all the features. If you need a classmate, please refer to it.

http://www.bkjia.com/PHPjc/631293.html www.bkjia.com true http://www.bkjia.com/PHPjc/631293.html techarticle This article first introduces the definition of the array in PHP and the destruction of the array, and then use an example to illustrate the sorting method of one-dimensional data, the need for a friend can refer to, I hope you have ...

  • 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.