I wrote some basic PHP syntax, as well as form submission and processing. in this article, I wrote the usage of arrays and the processing of arrays and strings, I hope to help you: I wrote some basic PHP syntaxes and submitted and processed forms in the previous article, in this article, I wrote the usage of arrays and the processing of arrays and strings. I hope this will be helpful to you:
Multi-dimensional array:
- $ Arr1 = array ('a' => 'MD', 'B' => 'ad '...);
- $ Arr2 = array ('C' => 'SD', 'D' => 'AE '...);
- $ Arr = array ('us' => $ arr1, 'AB' => $ arr2 );
Use multi-dimensional array:
- Echo "{$ arr ['us'] ['A']}";
--- Arr1.php instance source code:
-
-
-
-
--- Arr. php instance source code:
-
-
-
-
- If (! Emptyempty ($ _ POST ['name'])
- {
-
- $ Name = stripslashes ($ _ POST ['name']);
-
- }
- Else
- {
-
- $ Nam = NULL;
-
- Echo'
You forgot to enter your name
';
-
- }
- If (isset ($ _ POST ['interests'])
- {
- $ Interests = true;
- }
- Else
- {
- $ Interests = NULL;
- Echo'
You forgot to enter your interests!
';
- }
-
- If ($ name & $ interests)
-
- {
-
- Echo "Thank you,$ Name, You entered your interests:
";
-
- Foreach ($ _ POST ['interests'] as $ value)
-
- {
-
- Echo"
- $ Value
N ";
-
- }
-
- Echo'
';
-
- }
-
- Else
-
- {
-
- Echo'
Please go back and fill out the form again.
';
-
- }
- ?>
-
Array and string
Array and string conversion:
- $ Array = explode (separator, $ string );
- $ String = implode (glue, $ array );
Separator (separator) and glue. When an array is converted into a string, it is set to glue-the characters and code inserted into the array value in the generated string will be, when converting a string into an array, a delimiter is used to describe the code that generates links between different elements in the array.
The PHP instance source code is as follows:
- $ String1 = 'mon-Tue-Wed-Thur-fri ';
- $ Days = explode ('-', $ string1 );
- $ Days has five elements: 0 => 'Mon '...
- $ String2 = implode (',', $ days );
- // $ String2 is a comma-separated list of days in a week: Mon, Tue ...;
In the preceding example, the arr. php part can be changed:
- If (isset ($ _ POST ['interests']) {
- $ Interests = implode (',', $ _ POST ['interests']);
- }
Then, $ interests can be output as a string.
Summary:
Synonym of implode () function in join () function
The third optional parameter of the explode () function is a number used to limit the number of array elements to be created.
Array sorting:
You can use sort () to sort the array by value, but it discards the original key. after the sorting process, the array key is reset. therefore, if the key-value relationship is very important, it is best not to use this function.
Sort by value with asort () to maintain the key.
You can use ksort () to sort arrays.
If you change the preceding functions to rsort (), arsort (), and krsort (), you can sort the arrays in reverse mode.
---- The source code of the arrsort. php instance is as follows:
-
-
- $ Movies = array (10 => 'CA', 9 => 'to', 2 => 'The ', 8 => 'sideway ', 7 => 'donni ');
- Echo'
In their original order:
Rating Title:
';
- Foreach ($ movies as $ key => $ value)
- {
- Echo "$ keyt $ valuen ";
- }
- Echo'';
- Echo'
Sorted by title:
Rating Title:
';
- Asort ($ movies); // sort the array by value.
- Foreach ($ movies as $ key => $ value)
- {
- Echo "$ keyt $ valuen ";
- }
- Echo'';
- Echo'
Sorted by rating:
Rating Title:
';
- Krsort ($ movies); // Sort by pressing the buttons and invert the array position.
- Foreach ($ movies as $ key => $ value)
- {
- Echo "$ keyt $ valuen ";
- }
- Echo'';
- ?>
-