Some basic PHP syntax (array, string)

Source: Internet
Author: User
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:

  1. $ Arr1 = array ('a' => 'MD', 'B' => 'ad '...);
  2. $ Arr2 = array ('C' => 'SD', 'D' => 'AE '...);
  3. $ Arr = array ('us' => $ arr1, 'AB' => $ arr2 );

Use multi-dimensional array:

  1. Echo "{$ arr ['us'] ['A']}";

--- Arr1.php instance source code:

  1.   
  2.  
  3.  
  4.  

--- Arr. php instance source code:

  1.  
  2.  
  3.  
  4. If (! Emptyempty ($ _ POST ['name'])
  5. {
  6.  
  7. $ Name = stripslashes ($ _ POST ['name']);
  8.  
  9. }
  10. Else
  11. {
  12.  
  13. $ Nam = NULL;
  14.  
  15. Echo'

    You forgot to enter your name

    ';
  16.  
  17. }
  18. If (isset ($ _ POST ['interests'])
  19. {
  20. $ Interests = true;
  21. }
  22. Else
  23. {
  24. $ Interests = NULL;
  25. Echo'

    You forgot to enter your interests!

    ';
  26. }
  27.  
  28. If ($ name & $ interests)
  29.  
  30. {
  31.  
  32. Echo "Thank you,$ Name, You entered your interests:
      ";
    •  
    • Foreach ($ _ POST ['interests'] as $ value)
    •  
    • {
    •  
    • Echo"
    • $ Value
    • N ";
    •  
    • }
    •  
    • Echo'
    ';
  33.  
  34. }
  35.  
  36. Else
  37.  
  38. {
  39.  
  40. Echo'

    Please go back and fill out the form again.

    ';
  41.  
  42. }
  43. ?>
  44.  

Array and string

Array and string conversion:

  1. $ Array = explode (separator, $ string );
  2. $ 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:

  1. $ String1 = 'mon-Tue-Wed-Thur-fri ';
  2. $ Days = explode ('-', $ string1 );
  3. $ Days has five elements: 0 => 'Mon '...
  4. $ String2 = implode (',', $ days );
  5. // $ String2 is a comma-separated list of days in a week: Mon, Tue ...;

In the preceding example, the arr. php part can be changed:

  1. If (isset ($ _ POST ['interests']) {
  2. $ Interests = implode (',', $ _ POST ['interests']);
  3. }
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:

  1.  
  2. $ Movies = array (10 => 'CA', 9 => 'to', 2 => 'The ', 8 => 'sideway ', 7 => 'donni ');
  3. Echo'

    In their original order:

    Rating Title:
    '; 
  4. Foreach ($ movies as $ key => $ value)
  5. {
  6. Echo "$ keyt $ valuen ";
  7. }
  8. Echo'

    ';
  9. Echo'

    Sorted by title:

    Rating Title:
    '; 
  10. Asort ($ movies); // sort the array by value.
  11. Foreach ($ movies as $ key => $ value)
  12. {
  13. Echo "$ keyt $ valuen ";
  14. }
  15. Echo'

    ';
  16. Echo'

    Sorted by rating:

    Rating Title:
    '; 
  17. Krsort ($ movies); // Sort by pressing the buttons and invert the array position.
  18. Foreach ($ movies as $ key => $ value)
  19. {
  20. Echo "$ keyt $ valuen ";
  21. }
  22. Echo'

    ';
  23. ?>
  24.  

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.