Php array definition and initialization learning Notes

Source: Internet
Author: User
What is an array? An array is a programming structure. it is a variable that stores a group or a series of numerical values. for example, it can be used to register an individual's identity during census, such as name, gender, ethnicity, birth, etc.

What is an array? An array is a programming structure. it is a variable that stores a group or a series of numerical values, such as name, gender, ethnicity, birth, etc. can be used as an array.

The array created in php is defined using the array () structure, for example:

$ People = array ('name', 'sex', 'nation', 'brith ');

To display the values of each element in an array, we use an index starting from 0. The index number is in square brackets after the variable name. for example:

  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. Echo $ people [2];
  3. ?>

The output $ people [2] indicates that nation (the first index item is counted from 0) is displayed. in addition to the numerical index array, php also supports related arrays, so-called related arrays, A custom keyword can be used to replace an unintuitive Digital Index, for example:

  1. $ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
  2. Echo $ doneles ['CS '];
  3. ?>

The related array is used to make the output selection intuitive (no pre-calculation of index numbers is required and then output). the defined keywords and values are defined using the "=>" symbol.

Based on the two display methods of php array elements, you can also create numbers automatically without the need for array () declaration and initialization like variables, such:

  1. $ People [0] = 'name ';
  2. $ People [1] = 'sex ';
  3. $ People [2] = 'Nation ';
  4. $ People [3] = 'brith ';
  5. // Or
  6. $ Les ['xm '] = 'name ';
  7. $ Doneles ['xb '] = 'sex ';
  8. $ Doneles ['mz'] = 'Nation ';
  9. $ Les ['CS '] = 'birth'; // The size of the array varies dynamically based on the number of elements added.

Display of array elements:

In the above example, no matter $ people [2] or $ minutes les ['CS '], only an array element value with a known clear position is output, how to quickly output all or part of the array elements is undoubtedly the fastest way to use loop statements:

  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. For ($ I = 0; $ I <4; $ I ++)
  3. Echo "$ people [$ I]";
  4. ?>

In addition to the for loop that understands the number of cycles, you can also use a foreach statement that does not require the number of cycles:

  1. $ People = array ('name', 'sex', 'nation', 'birth ');
  2. Foreach ($ people as $ xiangmu)
  3. Echo $ xiangmu;
  4. ?>

The $ xiangmu variable will save the values of each element in the array and display them in sequence. of course, to differentiate the output data at intervals, a space can be output after the array element:

Echo $ xiangmu. ""; now let's take a look at a php to filter out repeated data from the array:

  1. $ Num = count ($ array );
  2. If ($ num)
  3. {
  4. Sort ($ array );
  5. }
  6. If ($ num! = 0)
  7. {
  8. $ M = $ array [0];
  9. $ N = 0;
  10. $ Kind = 1;
  11. Echo $ array [0]. "----------";
  12. For ($ z = 0; $ z <$ num; $ z ++)
  13. {
  14. If ($ m! = $ Array [$ z])
  15. {
  16. Echo $ array [$ Z-1]. "";
  17. Echo $ array [$ z];
  18. $ Kind = 0;
  19. $ M = $ array [$ z];
  20. }
  21. $ N ++;
  22. $ M ++;
  23. // Echo $ array [$ z]. "'/n '";
  24. }
  25. If ($ kind = 1)
  26. {
  27. Echo $ array [$ num-1]. "duplicate data! ";
  28. }
  29. ?>

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.