Deep learning about how to delete elements from PHP arrays

Source: Internet
Author: User
We use the unset () function to solve the PHP array deletion problem. The article provides detailed code and explanations, hoping to improve the technology. "> <LINKhref =" http://www.php100.com//statics/style/headfloor_950_081205.css "type = text/cssrel = stylesheet> <L

 

When learning PHP, you may encounter the PHP array deletion problem. here we will introduce the solution to the PHP array deletion problem, and share it with you here. Anyone who has learned the C language may know that the C language is very powerful in string processing. PHP is written in C. Naturally, it inherits the advantage of C in string processing. After all, PHP is a new language, which is different from the C language. Naturally, PHP cannot be exactly the same as C. Therefore, some functions can only be known after testing. Sometimes each character of a string needs to be processed. the general practice may be:


  1. $ Str = "something ";
  2. For ($ I = 0; $ I
  3. {  
  4. $ Ch = substr ($ str, $ I, 1 );
  5. // Process $ ch

This is acceptable, but is there a more elegant way? Yes, it is treated as an array, and the C language is like this.

Let's change the above example to a string array processing method:


  1. $ Str = "something ";
  2. For ($ I = 0; $ I
  3. {  
  4. $ Ch = $ str [$ I];
  5. // Process $ ch

This is not much better.

Delete elements from PHP arrays

Define an array. what if I want to delete several items in the PHP array? I saw an answer in www.phpbuilder.com, that is, using the unset () function. Let's do a test.


  1. $ A [] = "a1 ";
  2. $ A [] = "a2 ";
  3. $ A [] = "a3 ";
  4. For ($ I = 0; $ I
  5. {  
  6. Echo $ a [$ I]."
  7. ";
  8. }  
  9. Unset ($ a [0]);
  10. For ($ I = 0; $ I
  11. {  
  12. Echo $ a [$ I]."
  13. ";

 

  • Summary of PHP array functions
  • Learn more about using PHP array functions
  • Case study: PHP array query
  • Code to sort PHP arrays
  • Comparison test of PHP array traversal

What does that mean? A $ a array with three elements is displayed. then, 1st elements (subscript: 0) are deleted and displayed. The result should be two elements left in the array. But no! The answer is different from what we think. isn't unset () difficult to use? Think about it. The original unset ($ a [0]) deleted 1st elements, but we still started from $ I = 0 in the output. of course, this is incorrect, php does not automatically adjust the lower mark. In this way, we have to deal with it in other ways:

 


  1. $ A [] = "a1 ";
  2. $ A [] = "a2 ";
  3. $ A [] = "a3 ";
  4. For ($ I = 0; $ I
  5. {  
  6. Echo $ a [$ I]."
  7. ";
  8. }  
  9. Unset ($ a [0]);
  10. Reset ($ a); // returns the array pointer to 1st elements.
  11. While (list ($ c, $ d) = each ($ ))
  12. {  
  13. Echo $ d ."
  14. "; // $ C is an array subscript

This is a common method for displaying arrays. you do not need to consider the subscript of arrays.

Related Article

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.