Array-related functions in collected PHP

Source: Internet
Author: User
Tags array definition
The array-related functions in the collected PHP are from the beginning of ASP to PHP. I feel that one of the most powerful PHP functions is the rich built-in functions, such as the PHP date and time functions I learned earlier, all functions related to file reading and writing indicate that PHP is more professional and user-friendly.
At first, I was very excited about the rich functions of PHP functions. as more and more abnormal functions become available, I suddenly think of ASP built-in functions as rare. to complete a special function, user-defined functions are often required. as the number of applications increases, you actually have a set of common function libraries. However, in PHP, these functions have already been standardized and concentrated for direct use of built-in functions. ASP developers have become common PHP users.
However, the existence of these functions and a large number of PHP functions at least shows that PHP is more professional. at the same time, it should be fast and easy to use in our daily PHP program processing, this frees developers from customizing functions for basic functions and detailed functions, and focuses on building more powerful program modules. Therefore, I have strengthened my belief in viewing PHP functions, but I think the PHP function manual should be a portable book in the future development process.
Of course, there is no need to discuss the debate over ASP and PHP advantages and disadvantages. learning and understanding can help you understand the truth.
As the saying goes, there are too many PHP functions to prevent forgetting, so I will make a summary and collection every time after reading a class of functions, so I will write a log for convenience.
1. array definition and initialization
What is an array? An array is a programming structure. it is a variable that stores a group or a series of values.
For example, personal identity registration during census, such as name, gender, ethnicity, and birth, 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:
$ People = array ('name', 'sex', 'nation', 'birth ');
Echo $ people [2];
?>
The output $ people [2] indicates that nation is displayed (the first index item is counted from 0 ).
In addition to numeric index arrays, PHP also supports related arrays. Related arrays are used to replace unintuitive numeric indexes with custom keywords. for example:
$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
Echo $ doneles ['CS '];
?>
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. For example
$ People [0] = 'name ';
$ People [1] = 'sex ';
$ People [2] = 'Nation ';
$ People [3] = 'brith ';
Or
$ Les ['xm '] = 'name ';
$ Doneles ['xb '] = 'sex ';
$ Doneles ['mz'] = 'Nation ';
$ Les ['CS '] = 'birth ';
The size of the array varies dynamically according to the number of added elements.
2. 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.
$ People = array ('name', 'sex', 'nation', 'birth ');
For ($ I = 0; $ I <4; $ I ++)
Echo "$ people [$ I]";
?>
In addition to the for loop that understands the number of loops, you can also use a foreach statement that does not require the number of loops.
$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ xiangmu)
Echo $ xiangmu;
?>
The $ xiangmu variable saves the element values in the array and displays them in sequence. Of course, in order to differentiate the output data at intervals, spaces can be output after the array elements:
Echo $ xiangmu ."";
Note: The English periods (.) can be used to merge string connections into new strings. For more information, see closely following PHP variables and constant learning notes.

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.