Php array definition and variable destruction and one-dimensional array sorting

Source: Internet
Author: User
This article first introduces the definition of arrays in php and the destruction of arrays, and then uses an instance to describe how to sort the Dimension Data. PHP array definition, code: $ arrayarray (& amp; 39; o

This article first introduces the definition of arrays in php and the destruction of arrays, and then uses an instance to describe how to sort the Dimension Data.

PHP array definition,The code is as follows:

  1. $ Array = array ('one', 'two', 'Three ');
  2. Var_dump ($ array );

In the first line of the code snippet, a one-dimensional array $ array is defined. in the second line, the array is formatted and output. The result is as follows:

  1. Array (3 ){
  2. [0] =>
  3. String (3) "one"
  4. [1] =>
  5. String (3) "two"
  6. [2] =>
  7. String (5) "three"
  8. }

Now let's explain the output result. First, the first line of the output result of this array. array (3) tells us that this is an array with three elements, element 0 is a 3-character string (3 ))... Let's not talk about the remaining two links ......, What does this mean? This means that if we do not specify a subscript for the array in PHP, he will give us a subscript from scratch, that is, the key name in the PHP array; let's take a look at the example below:

  1. $ Array = array ('one', "hello" => 'two', 'Three ');
  2. Var_dump ($ array );

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

  1. Array (3 ){
  2. [0] =>
  3. String (3) "one"
  4. ["Hello"] =>
  5. String (3) "two"
  6. [1] =>
  7. String (5) "three"
  8. }

I think you see a very intelligent phenomenon like me. the key name of the first element is still 0. we can understand this because we didn't specify it, the PHP array must have a key name, and PHP creates a key name from scratch. when the second element is used, we specify the key name, PHP respected our opinion and used this key name. complicated things are in the third element and the third element. it looks very simple. we didn't specify a key name, PHP automatically adds 1 to the maximum integer key name as the key name. But you have never thought about it. if we change the key name of the first element to "-5" and the key name of the second element remains unchanged, what will the result be? The code is as follows:

  1. $ Array = array (-5 => 'one', "hello" => 'two', 'Three ');
  2. Var_dump ($ array );

If you take it for granted that the key name of the third element should be-4, I will tell you that this idea was correct before PHP4.3.0, but then it will be wrong, now, PHP4.3.0 and later versions, you will see the following results:

  1. Array (3 ){
  2. [-5] =>
  3. String (3) "one"
  4. ["Hello"] =>
  5. String (3) "two"
  6. [0] =>
  7. String (5) "three"
  8. }

Yes, the third element starts from 0, that is, no matter how small your negative number is, if the next element asks PHP to define the key name, then he starts from 0. remember that in the current key name, if the maximum value is still a negative number, no matter how small the negative number is, PHP is also the next key name from scratch.

The destruction of PHP arrays is simple, just like destroying other variables.

Destroy the entire array: unset ($ array)

Destroy an element in the array: unset ($ array [-5])

In real life or in the world of programs, order is always important-I can't imagine what a world without order will look like! PHP array is no exception. PHP provides four groups of sorting functions for PHP arrays. the first three are for one-dimensional arrays. let's first talk about these three types. The fourth sorting method for multi-dimensional arrays is described in the next article, that is a little complicated.

Group 1: sort and rsort, which are sorted by asc and desc in the order of PHP array key values, at the same time, the index relationship of the original array is damaged. In fact, after the index is deleted, the numeric index starting from 0 is re-created. Take a look at the routine and the code is as follows:

  1. $ A = array ("a" => 1, 2 );
  2. Sort ($ );
  3. Var_dump ($ );
  4. Rsort ($ );
  5. Var_dump ($ );
  6. ?>

Let's take a look at the first output result, and the first output:

  1. Array (2 ){
  2. [0] =>
  3. Int (1)
  4. [1] =>
  5. Int (2)
  6. }

Second output:

  1. Array (2 ){
  2. [0] =>
  3. Int (5)
  4. [1] =>
  5. Int (4)
  6. }

Where did we find that index a was not defined? Where are you going? It is certain that they have been relentlessly deleted. if you don't care about the original index relationship, you can use them!

The second group of functions: asort and arsort. These two functions are more powerful. as long as they can retain the original index relationship of the array, replace the sort and rsort functions in the above example with the two functions respectively, view the running result:

  1. Array (2 ){
  2. ["A"] =>
  3. Int (1)
  4. [0] =>
  5. Int (2)
  6. }
  7. Array (2 ){
  8. [0] =>
  9. Int (2)
  10. ["A"] =>
  11. Int (1)
  12. }

You can understand it at a glance!

The third group of PHP array sorting functions: krsort and ksort are different from the above two groups. These two functions sort key names. you can replace the functions in the above example with the two, let's take a look at the specific running Results. Otherwise, this article will be written too long. I'm afraid some of my brothers will not be patient enough to see the focus of this article, although the focus is below!

Sort PHP arrays by using custom functions. the following three functions are available:

Uasort sorts the key values of the PHP array using a custom function and retains the original index relationship.

Uksort sorts the key names of PHP arrays by using a custom function and retains the original index relationship.

Usort sorts the key values of the PHP array through a user-defined function, deletes the original index relationship, and creates a new index from scratch.

Here, of course, we need an example. First, we need a function that accepts two parameters and returns a certain value. if the first parameter is equal to the second parameter, 0 is returned, if the value is smaller than-1, the return value is greater than 1. the code is as follows:

  1. Function cmp ($ a, $ B ){
  2. $ A + = 1;
  3. $ B + = 3; // change these values and compare them.
  4.  
  5. If ($ a = $ B) return 0;
  6. Return ($ a <$ B )? -1:1;
  7. }
  8.  
  9. $ A = array (1, 4, 3, 5 );
  10. Uasort ($ a, 'cmp ');
  11. Var_dump ($ );

Output result:

  1. Array (4 ){
  2. [0] =>
  3. Int (1)
  4. [3] =>
  5. Int (5)
  6. [1] =>
  7. Int (4)
  8. [2] =>
  9. Int (3)
  10. }

Haha ...... Is it worse than not sorting? Here we only demonstrate the method used. let's do it yourself when you use it! If you do not compare these values, for example, here:

$ A + = 1; $ B + = 3; // compare after changing these values

In the PHP Manual, a total of 74 array functions are defined, covering the definition, assignment, sorting, numerical operation, comparison of PHP arrays, and key names for arrays, key value reversal ...... And so on.

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.